Prerequisites

Creating an express server

1

Install

For ExpressJS installation, refer to their documentation

Now install the @unkey/api package

npm install @unkey/api
2

Creating the server

Create a file called server.ts and add the following code

server.ts
import express, { Request, Response, Application } from 'express';
import dotenv from 'dotenv';
import { verifyKey } from '@unkey/api';
//For env File
dotenv.config();

const app: Application = express();
const port = process.env.PORT || 8000;

app.get('/', (req: Request, res: Response) => {
  res.send('Welcome to Express & TypeScript Server');
});

// This endpoint is protected by Unkey
app.get('/secret', async (req: Request, res: Response) => {
  const authHeader = req.headers["authorization"]
  const key = authHeader?.toString().replace("Bearer ", "");
  if (!key) {
    return res.status(401).send("Unauthorized")
  }

  const { result, error } = await verifyKey(key);
  if (error) {
    // This may happen on network errors
    // We already retry the request 5 times, but if it still fails, we return an error
    console.error(error);
    res.status(500);
    return res.status(500).send("Internal Server Error")
  }

  if (!result.valid) {
    res.status(401);
    return res.status(401).send("Unauthorized")
  }

  return res.status(200).send(JSON.stringify(result));
})
app.listen(port, () => {
  console.log(`Server is listening at http://localhost:${port}`);
});
3

Running the server

npm run start
4

Try it out

Go to https://app.unkey.com and create a new key. Then verify it with our new server:

curl 'http://localhost:8000/secret' \
  -H 'Authorization:Bearer <YOUR_KEY>'

It should return { "valid": true } and potentially more information about the key, depending on what you set up in the dashboard.