Handling Secrets in SST
In the previous chapter, we created a Stripe account and got a pair of keys. Including the Stripe secret key. We need this in our app but we do not want to store this secret in our code. In this chapter, we’ll look at how to add secrets in SST.
We will be using the sst secret CLI to store our secrets.
Run the following in your project root.
$ npx sst secret set StripeSecretKey <YOUR_STRIPE_SECRET_TEST_KEY>
You can run npx sst secret list to see the secrets for the current stage.
Now that the secret is stored, we can add it into our config using the Secret component.
Add the following to your infra/storage.ts:
// Create a secret for Stripe
export const secret = new sst.Secret("StripeSecretKey");
Import secret in infra/api.ts. Replace the following.
import { table } from "./storage";
With:
import { table, secret } from "./storage";
Next, link StripeSecretKey to the API in infra/api.ts. Replace this:
link: [table],
With:
link: [table, secret],
This will add StripeSecretKey in our infrastructure. And allow our API to access the secret.
Now we are ready to add an API to handle billing.
For help and discussion
Comments on this chapter