You need to have AWS credentials configured locally.
Let’s create a new SST + Next.js application.
npx create-sst@latest --template standard/nextjs
cd my-sst-app
npm install
npm install @upstash/redis
npx sst secrets set UPSTASH_REDIS_REST_URL <YOUR_URL>
npx sst secrets set UPSTASH_REDIS_REST_TOKEN <YOUR_TOKEN>
import { Config, StackContext, NextjsSite } from "sst/constructs";
export function Default({ stack }: StackContext) {
const UPSTASH_REDIS_REST_URL = new Config.Secret(stack, "UPSTASH_REDIS_REST_URL");
const UPSTASH_REDIS_REST_TOKEN = new Config.Secret(stack, "UPSTASH_REDIS_REST_TOKEN");
const site = new NextjsSite(stack, "site", {
bind: [UPSTASH_REDIS_REST_URL, UPSTASH_REDIS_REST_TOKEN],
path: "packages/web",
});
stack.addOutputs({
SiteUrl: site.url,
});
}
/packages/web/pages/api/hello.ts
import { Redis } from "@upstash/redis";
import type { NextApiRequest, NextApiResponse } from "next";
import { Config } from "sst/node/config";
const redis = new Redis({
url: Config.UPSTASH_REDIS_REST_URL,
token: Config.UPSTASH_REDIS_REST_TOKEN,
});
export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
const count = await redis.incr("counter");
res.status(200).json({ count });
}
Run the SST app.
After prompted, run the Next.js app.
cd packages/web
npm run dev
Set the secrets for the prod stage.
npx sst secrets set --stage prod UPSTASH_REDIS_REST_URL <YOUR_URL>
npx sst secrets set --stage prod UPSTASH_REDIS_REST_TOKEN <YOUR_TOKEN>
Deploy with SST.
npx sst deploy --stage prod