A simple TypeScript API that returns random data with Unkey authentication.
- 🔐 Unkey API key authentication
- 📊 Random data generation (users, products, quotes, jokes)
- ⚡ Fast and lightweight
- 🛠️ TypeScript with strict type checking
- 📝 Code style enforced with Biome
- Node.js 22+ (the repo pins pnpm 11 via
packageManager, which needs a modern Node) - pnpm (
corepack enablewill pick up the pinned version)
pnpm installCopy .env.example to .env and add your Unkey credentials:
cp .env.example .envUpdate the .env file with your actual Unkey credentials:
MY_ROOT_KEY: Your Unkey root key (get it from https://unkey.com)UNKEY_API_URL(optional): Base URL of the Unkey API. Defaults tohttps://api.unkey.com. Set this to test against a local or staging deployment — e.g.http://localhost:7070for the local Tilt stack (unkey/api:dev), whose dashboard runs athttp://localhost:3000.
The root key must belong to whichever deployment UNKEY_API_URL points at — a production root key
will not verify against a local instance, and vice versa. The server prints its target on startup.
pnpm devThe server will start on http://localhost:8080 (or the PORT specified in .env).
Note: if something else on your machine already owns port 8080 (Tilt, for example), the server will still report "Server running on port 8080" while requests to
localhost:8080go to the other process. SetPORTto a free port to avoid this.
curl http://localhost:8080/health
curl -H "Authorization: Bearer <a key from your Unkey API>" http://localhost:8080/api/me/api/me returns what Unkey reported for the key, which makes it a quick way to confirm a
deployment is talking to the right API and root key.
docker build -t simple-unkey-api .docker run -p 8080:8080 \
-e MY_ROOT_KEY=your_unkey_root_key_here \
simple-unkey-apiOr using a .env file:
docker run -p 8080:8080 --env-file .env simple-unkey-apiGET /- API information and available endpointsGET /health- Health check endpoint
All endpoints under /api/* require a valid Unkey API key in the Authorization header.
GET /api/users?count=5- Get random usersGET /api/products?count=5- Get random productsGET /api/quotes?count=1- Get random quotesGET /api/jokes?count=1- Get random jokesGET /api/random- Get a random item of any typeGET /api/me- Echo Unkey's verification result for the calling key (keyId, name, credits, permissions)
Include your Unkey API key in the Authorization header:
curl -H "Authorization: Bearer your_api_key_here" http://localhost:8080/api/usersKeys are checked against POST https://api.unkey.com/v2/keys.verifyKey using the MY_ROOT_KEY
root key. Responses you may get back:
| Status | Meaning |
|---|---|
200 |
Key verified, request served |
401 |
Missing header, or Unkey reported the key as invalid (the reason field carries Unkey's code, e.g. NOT_FOUND, EXPIRED, RATE_LIMITED, USAGE_EXCEEDED, DISABLED) |
500 |
MY_ROOT_KEY is not set on the server |
502 |
Unkey was unreachable or rejected the root key (the details field carries Unkey's message) |
curl -H "Authorization: Bearer your_api_key_here" http://localhost:8080/api/users?count=10curl -H "Authorization: Bearer your_api_key_here" http://localhost:8080/api/products?count=5curl -H "Authorization: Bearer your_api_key_here" http://localhost:8080/api/quotescurl -H "Authorization: Bearer your_api_key_here" http://localhost:8080/api/jokes?count=3pnpm buildpnpm startpnpm lintpnpm lint:fix- TypeScript
- Express.js
- Unkey for authentication
- Biome for linting and formatting
- pnpm for package management