SvelteKit
Amplify now puts hosting, a Postgres database, and sign-in in one AWS account. Deploy from your terminal or from Git, check every change in a preview first, and roll back in one step if something breaks.
Looking for Amplify Hosting? See the Amplify Hosting User Guide.
SvelteKit is a framework for building web applications with Svelte. Amplify supports SvelteKit through a dedicated adapter that integrates backend capabilities into your SvelteKit apps.
Why SvelteKit + Amplify?
-
Reactivity: Svelte’s approach to reactivity eliminates the virtual DOM overhead.
-
Performance: SvelteKit’s build output works with Amplify’s edge deployment.
-
Developer experience: Svelte provides a concise syntax, and SvelteKit provides filesystem-based routing.
-
Progressive enhancement: SvelteKit’s form actions and page options support both JavaScript and no-JavaScript experiences.
Prerequisites
-
Node.js 22 or later
-
npm 10 or later
-
AWS credentials. For how to set them up, see Configuring settings for the AWS CLI. A Git repository is optional.
Getting started
Start by creating a new SvelteKit app:
# Create a new SvelteKit app npx sv create my-sveltekit-app cd my-sveltekit-app
Follow the prompts to set up your SvelteKit app with your preferred configuration options.
Next, add Amplify to your existing SvelteKit app:
# Add Amplify to your SvelteKit app aws amplifygen3 create
The create command detects that you’re using SvelteKit and generates an amplify.jsonc file for your app. It then offers to install the SvelteKit adapter, which the build requires. If you decline, or your shell can’t answer the prompt (as in an agent session or CI), create prints the steps instead. To accept without a prompt, add --yes.
To finish the adapter setup, do the following. If create already installed the adapter package, skip the install step.
-
Install the adapter as a development dependency:
npm install -D @aws-amplifygen3/adapter-sveltekit -
In
vite.config.ts, import the adapter and pass it to thesveltekit()plugin. SvelteKit 3, whichnpx sv createsets up, has nosvelte.config.js. This needs version 0.1.9 or later of the adapter.import { sveltekit } from "@sveltejs/kit/vite" import adapter from "@aws-amplifygen3/adapter-sveltekit" import { defineConfig } from "vite" export default defineConfig({ plugins: [sveltekit({ adapter: adapter() })], })On SvelteKit 2, set the adapter as
kit.adapterinsvelte.config.jsinstead.
Configuring Amplify for SvelteKit
The generated amplify.jsonc file builds your app and writes the output to the .amplify directory:
{ "name": "my-sveltekit-app", "deploy": { "install": "npm ci", "build": "npm run build", "output_directory": ".amplify", }, }
Use a backend with SvelteKit
If your app has an Amplify backend, your SvelteKit code calls it with the client SDK, @aws-amplifygen3/backend-client-js, which needs Node.js 22.12 or later. Amplify creates and runs the backend. You own and manage any database that you connect yourself. For those, see Use integrations in SvelteKit and Working with a third-party database. To add a backend and install the SDK, see Amplify backend and the client SDK README
Each deployment and aws amplifygen3 local run set AMPLIFY_BACKEND_URL and AMPLIFY_BACKEND_PUBLISHABLE_KEY for your build and your dev server. SvelteKit builds with Vite, so add a define next to plugins in vite.config.ts that inlines both values into browser code:
// vite.config.ts import { defineConfig } from 'vite' export default defineConfig({ // Keep your existing plugins entry. define: { 'import.meta.env.AMPLIFY_BACKEND_URL': JSON.stringify(process.env.AMPLIFY_BACKEND_URL ?? ''), 'import.meta.env.AMPLIFY_BACKEND_PUBLISHABLE_KEY': JSON.stringify(process.env.AMPLIFY_BACKEND_PUBLISHABLE_KEY ?? ''), }, })
Leave server.port out of vite.config.ts. A port that the config pins is used even when another program holds it, which stops the dev server.
Create one client on first use:
import { createClient } from '@aws-amplifygen3/backend-client-js' let client: ReturnType<typeof createClient> | undefined export function backend() { if (!client) { client = createClient(import.meta.env.AMPLIFY_BACKEND_URL, import.meta.env.AMPLIFY_BACKEND_PUBLISHABLE_KEY) } return client }
Call backend() only from browser code, such as onMount or an event handler. A page that SvelteKit renders on the server has no browser session, so don’t call it while the page renders. In +page.server.ts or +server.ts, createClientFromEnv() reads both variables from process.env. To read the signed-in user in server code, see Server-side rendering.
To develop locally, run aws amplifygen3 local run. It starts the local backend and runs vite dev on the first free port from 5173, with --strictPort, and the local backend trusts that origin. A dev server that you start with npm run dev gets neither variable, and the local backend doesn’t trust it.
Use integrations in SvelteKit
Amplify gives your app each integration’s connection details as environment variables, such as BUCKET_NAME for Amazon S3, PGHOST for Amazon Aurora DSQL, and BEDROCK_MODEL_ID for Amazon Bedrock. Each integration page lists its variables.
Read them with process.env in server-only code: +server.ts endpoints, +page.server.ts load functions, and hooks.server.ts. Integration variables aren’t available in client code. To use a value in the browser, return it from a server load function. The page gets it in data.
The following endpoint queries Amazon Aurora DSQL on the server. Install the packages first:
npm install @aws/aurora-dsql-node-postgres-connector drizzle-orm pg
// src/routes/api/users/+server.ts import { json } from "@sveltejs/kit" import type { RequestHandler } from "./$types" import { AuroraDSQLPool } from "@aws/aurora-dsql-node-postgres-connector" import { drizzle } from "drizzle-orm/node-postgres" import { sql } from "drizzle-orm" const pool = new AuroraDSQLPool({ host: process.env.PGHOST, user: process.env.PGUSER, }) const db = drizzle({ client: pool }) export const GET: RequestHandler = async ({ url }) => { const id = url.searchParams.get("id") const { rows } = await db.execute(sql`SELECT * FROM users WHERE id = ${id}`) return json(rows) }
This code works for the following reasons:
-
The integration sets
PGHOSTandPGUSER. The Aurora DSQL connector reads them. Aurora DSQL has no password. The connector generates a short-lived IAM authentication token for each connection, and it refreshes the token for you. -
sqlis a template tag from Drizzle. Drizzle turns the template into a query with parameters. A value in${…}, such asid, goes to the database as a parameter and not as part of the SQL text, so the value can’t change the query. -
db.executesends the query through the connection pool and returns the rows.
If the browser must read a value, create a custom integration and give the variable a PUBLIC_ prefix. For code that uses each service, see Working with a third-party database, Amazon Aurora DSQL, Amazon S3, and Amazon Bedrock.
Working with a third-party database
This section is for a database that Amplify doesn’t create, such as a Postgres database from a third-party provider, or a database in your VPC. For an Amazon Aurora DSQL cluster, see the example in Use integrations in SvelteKit. If you use an Amplify backend, use the backend client library instead. See Use a backend with SvelteKit.
Connect the database to your app as a custom integration. Define the connection variables that the postgres library reads: PGHOST, PGPORT, PGDATABASE, PGUSER, and PGPASSWORD. Amplify reads the secret PGPASSWORD from a dotenv file, so the password doesn’t appear on the command line. Replace the values with your own, and connect the integration to the preview environment:
aws amplifygen3 integration connect custom --name my-database \ --integ-env-var PGHOST=db.example.com \ --integ-env-var PGPORT=5432 \ --integ-env-var PGDATABASE=app \ --integ-env-var PGUSER=app_user \ --integ-secret-env-var PGPASSWORD --integ-secret-env-file .env.secrets \ --env preview
A custom integration connects only to an environment whose role permissions mode is full.
You can use the same approach to connect to a database in your VPC. First connect your app to the VPC, as described in Connect your app to private resources. Then set PGHOST to the private address of the database.
Then connect the integration to your other environments:
aws amplifygen3 integration connect --name my-database --env production
Read and write the database in +page.server.ts. The postgres library reads the PG* variables from process.env, so postgres() needs no arguments. The sql tag sends each ${…} value to the database as a parameter, not as part of the SQL text:
// src/routes/notes/+page.server.ts import { fail } from "@sveltejs/kit" import type { Actions, PageServerLoad } from "./$types" import postgres from "postgres" const sql = postgres() export const load: PageServerLoad = async () => { const notes = await sql`select * from notes order by created_at desc` return { notes } } export const actions: Actions = { default: async ({ request }) => { const content = (await request.formData()).get("content") if (!content || typeof content !== "string") { return fail(400, { error: "Content is required" }) } await sql`insert into notes (content) values (${content})` return { success: true } }, }
The page receives notes in data. A form on the page that posts to the default action adds a note. SvelteKit runs the load function again after the action, so the page shows the new note.
Deploying your SvelteKit + Amplify app
Deploy your app with a single command:
aws amplifygen3 deploy
Amplify builds and deploys your SvelteKit application to the edge. After deployment, you receive a unique URL for your site.
Local development
The Amplify adapter runs only when Amplify builds your app, so the SvelteKit development server doesn’t get your environment’s variables. To use them locally, download them to .env.local. The file includes your integrations' variables, and it writes secrets with empty values, so fill those in yourself:
aws amplifygen3 var download .env.local --env preview
Then start the development server with hot reloading:
# Start development server npm run dev
For an app with a backend, run aws amplifygen3 local run instead, which starts the local backend and your dev server with the backend’s URL and publishable key.