Back to Templates

Protecting your fastify API

API keys and ratelimiting for fastify

Written by
Vardhaman619
Framework
fastify
Language
Typescript
API keys and ratelimiting for fastify

Fastify Unkey Template

This project is a Fastify-based API template integrated with Unkey for API key management and rate-limiting. It uses OpenAI API to generate facts, and implements user authentication and request-limiting features.

Features

  • Fastify setup with TypeScript
  • User authentication with API key generation (via Unkey)
  • Rate-limiting with daily credit limits
  • Facts generation using OpenAI API
  • Turso as the database for storage
  • Organized services, controllers, and middleware

Prerequisites

Ensure you have the following installed:

  • Git
  • Bun >= 1.x
  • Turso database credentials (or any supported database, configurable in src/db/index.ts)
  • Unkey Acount for API key management
  • OpenAI API Key for facts generation

Setup Instructions

1. Clone the repository:

1git clone https://github.com/Vardhaman619/fastify-unkey.git
2cd fastify-unkey

2. Install dependencies:

1bun install

3. Configure environment variables:

Copy the .env.example file and create a .env file:

1cp .env.example .env

Ensure you fill in all the required environment variables such as:

1SERVER_PORT=3000
2OPENAI_API_KEY=your_openai_api_key
3UNKEY_API_KEY=your_unkey_api_key
4UNKEY_API_ID=your-unkey-api-id
5TURSO_DATABASE_URL=your-turso-database-url
6TURSO_AUTH_TOKEN=your-turso-auth-token

4. Database setup:

1bun db:generate
2bun db:migrate

5. Start the server:

1bun start

Usage Instructions

1. Authentication

To use the API, you need to generate an API key using Unkey. Once you have the API key, you can make requests to the API using the following format:

Request:

POST /signup

  • Description: Registers a new user and provides them with an API key.
  • Example Request Body:
1{
2 "email": "your_email@example.com",
3 "password": "your_password"
4}

Response:

1{
2 "apiKey": "your_api_key"
3}

2. Facts API

Request:

GET /fact

  • Description: Description: Fetches a random fact. The user must be authenticated with an API key, and rate-limited to 10 requests per day.
  • Pre-requisites: Add API Key in headers: Authorization: Bearer your-api-key

Request:

1{
2 "headers": {
3 "Authorization": "Bearer your-api-key"
4 }
5}

Response:

1{
2 "fact": "Did you know...?"
3}

Rate Limiting

Each user is limited to 10 requests per day. Once the limit is reached, the API will return a 429 (Too Many Requests) status code.

3. Get all facts

Request:

GET /facts

  • Description: Description: Returns all facts generated for today by the authenticated user.

  • Pre-requisites: Add API Key in headers: Authorization: Bearer your-api-key

Request:

1{
2 "headers": {
3 "Authorization": "Bearer your-api-key"
4 }
5}

Response:

1[
2 {
3 "fact": "Did you know...?",
4 "createdAt": "dd-mm-yyyy"
5 },
6 {
7 "fact": "Did you know...?",
8 "createdAt": "dd-mm-yyyy"
9 }
10]

Project Structure

  • src/
    • controllers/: Manages API request handling (auth and facts).
    • services/: Business logic related to authentication and facts.
    • middleware/: Implements rate limiting and authentication middleware.
    • plugins/: Fastify plugins for authentication and facts handling.
    • db/: Contains the database schema, migrations, and configuration.
    • lib/: Library functions for interacting with Unkey and OpenAI.

License

This project is licensed under the MIT License. See the LICENSE file for more information.

Protect your API.
Start today.

2500 verifications and 100K successful rate‑limited requests per month. No CC required.