Skip to content

SDK

Interact with your infrastructure in your runtime code.

The SST SDK allows your runtime code to interact with your infrastructure in a typesafe way.

You can use the SDK in your functions, frontends, and container applications. You can access links from components. And some components come with SDK clients that you can use.

Currently, the SDK is only available for JavaScript/TypeScript and Golang. Support for other languages is on the roadmap.


Node.js

The JS SDK is an npm package that you can install in your functions, frontends, or container applications.

Terminal window
npm install sst

Import Resource to access the linked resources.

src/lambda.ts
import { Resource } from "sst";
console.log(Resource.MyBucket.name);

Here we are assuming that a bucket has been linked to the function. Here’s what that could look like.

sst.config.ts
const bucket = new sst.aws.Bucket("MyBucket");
new sst.aws.Function("MyFunction", {
handler: "src/lambda.handler",
link: [bucket]
});

Defaults

By default, the Resource object contains Resource.App. This gives you some info about the current app including:

  • App.name: The name of your SST app.
  • App.stage: The current stage of your SST app.
src/lambda.ts
import { Resource } from "sst";
console.log(Resource.App.name, Resource.App.stage);

Clients

Components like the Realtime component come with a client that you can use.

src/lambda.ts
import { realtime } from "sst/aws/realtime";
export const handler = realtime.authorizer(async (token) => {
// Validate the token
});

For example, realtime.authorizer lets you create the handler for the authorizer function that Realtime needs.


How it works

In the above example, Resource.MyBucket.name works because it’s been injected into the function package on sst dev and sst deploy.

For functions, this is injected into the globalThis using esbuild and for frontends, it’s injected into the process.env object.

The JS SDK first checks the process.env and then the globalThis for the linked resources. You can read more about how the links are injected.


Golang

Use the SST Go SDK package in your Golang functions or container applications.

src/main.go
import (
"github.com/sst/sst/v3/sdk/golang/resource"
)

In your runtime code, use the resource.Get function to access the linked resources.

src/main.go
resource.Get("MyBucket", "name")

Where MyBucket is the name of a bucket that’s linked to the function.

sst.config.ts
const bucket = new sst.aws.Bucket("MyBucket");
new sst.aws.Function("MyFunction", {
handler: "./src",
link: [bucket]
});

You can also access the current app’s info with.

src/main.go
resource.Get("App", "name")
resource.Get("App", "stage")

Client functions are currently not supported in the Go SDK.