Skip to content

Build an API with SST

Create and deploy a serverless API in AWS with SST.

We are going to build a serverless API, add an S3 Bucket for file uploads, and deploy it to AWS using SST.

Before you get started:

  1. Configure your AWS credentials
  2. Install the SST CLI

1. Create a Node.js app

Let’s start by creating our app.

Terminal window
mkdir my-ts-app && cd my-ts-app
npm init -y

Init SST

Now let’s initialize SST in our app. Make sure you have the CLI installed.

Terminal window
sst init

This’ll create a sst.config.ts file in your project root.

2. Add an API

Let’s add an API using Amazon API Gateway. Update your sst.config.ts.

async run() {
.route("GET /", "index.upload")
.route("GET /latest", "index.latest");

We are adding two routes, one for uploading an image and another for getting the latest uploaded image.

Start dev mode

Start your app in dev mode. This runs your functions Live.

Terminal window
sst dev

This will give you the URL of your API.


3. Add an S3 Bucket

Let’s add a public S3 Bucket for file uploads. Update your sst.config.ts.

const bucket = new"MyBucket", {
public: true

Now, link the bucket to the API routes.

.route("GET /", {
link: [bucket],
handler: "index.upload"
.route("GET /latest", {
link: [bucket],
handler: "index.latest"

4. Upload a file

We want the / route of our API to generate a pre-signed URL to upload a file to our S3 Bucket. Create an index.ts file and add the following.

export async function upload() {
const command = new PutObjectCommand({
Key: crypto.randomUUID(),
return {
statusCode: 200,
body: await getSignedUrl(s3, command),

5. Download a file

We want the /latest route of our API to generate a pre-signed URL to download the last uploaded file in our S3 Bucket. Add this to your index.ts file.

export async function latest() {
const objects = await s3.send(
new ListObjectsV2Command({
const latestFile = objects.Contents!.sort(
(a, b) =>
(b.LastModified?.getTime() ?? 0) - (a.LastModified?.getTime() ?? 0)
const command = new GetObjectCommand({
Key: latestFile.Key,
return {
statusCode: 302,
headers: {
Location: await getSignedUrl(s3, command),

Add the relevant imports.

import { Resource } from "sst";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import {
} from "@aws-sdk/client-s3";
const s3 = new S3Client({});

And install the npm packages.

Terminal window
npm install sst@ion @aws-sdk/client-s3 @aws-sdk/s3-request-presigner

Test your app

Let’s try uploading a file from your project root. Make sure to use your API URL.

Terminal window
curl --upload-file package.json "$(curl"

Now head over to in your browser and it’ll download the file you just uploaded.

6. Deploy your app

Now let’s deploy your app.

Terminal window
sst deploy

Connect the console

As a next step, you can manage your app and view issues in the SST Console.

Issues in API app in SST Console

You can create a free account and connect it to your AWS account.