API key authentication in Svelte
This project demonstrates how you can protect your Svelte API Routes with Unkey
Unkey account
API ID
.access-unkeyed-route
withAuth
permission for the role.cp env.example .env
and add the following variables:
1UNKEY_API_ID=""2UNKEY_ROOT_KEY=""
Install all the dependencies
1pnpm install
Run the app. It should start at port 5173
1pnpm dev
Create a key using the following api/createKey route
1curl http://localhost:5173/api/createKey
Now access the protected route which verifies the key
1curl -H "Authorization: Bearer YOUR_API_KEY" http://localhost:5173/api/protected
2500 verifications and 100K successful rate‑limited requests per month. No CC required.