Novel
Get NovelGuidesAPI Reference
Guides
Guides
  • Guides
  • Production Checklist
  • Shipping Fast
    • Speedrun
    • Getting an Idea
    • Buying a Domain
    • Designing your SaaS
    • Pricing
    • Marketing and Distribution
    • Securing your SaaS
    • Troubleshooting
    • Optimizations
    • Integrating your Team
  • Knowledge Base
    • General Information
      • Getting Stripe Keys
      • Getting Postmark Keys
      • Getting Github Oauth Credentials
      • Using AWS S3
      • Using Cloudflare R2
      • Getting VAPID Keys
      • Getting Anthropic API Keys
      • Using Sentry
    • Novel Server
      • Configuring Novel
      • Email Providers
      • Configuring Pricing
      • Creating a REST Endpoint
      • Securing an Endpoint
      • Writing a Middleware
      • Background Jobs
      • Cron Jobs
      • Setting up Oauth
      • Integrating an OAuth Provider
      • Setting up Multi-Factor Authentication
      • Configure Roles and Permissions
      • Creating an Error File
      • Sending Emails
      • Setting up Signups
      • Writing Transactional Email
      • Writing End-to-end Tests
      • Creating a Custom Model
      • Overriding a Model
      • Creating a Feature Flag
      • Adding Rate Limits
      • Requiring Elevated Permission
      • Using Idempotency
      • Check if User is Verified
      • Check if Organization is Subscribed
      • Caching an Endpoint
      • Setting up Cloudflare R2
    • Database
      • Connecting to a Database
      • Creating a Table
      • Writing Queries
      • Writing Mutations / Updates / Deletes
      • Using Validations
    • Novel Web / Next.js
      • Creating a Marketing Page
      • Creating an Application Screen
      • Securing a Page
      • Making a Request to the API
      • Using Generated Requests
      • Applying Styles
      • Using shadcn/ui
      • Writing Tests with Playwright
      • Modifying Content Security Policy
      • Checking a Feature Flag
      • Using Zod in Forms
    • Scaffolding
      • Describing a Background Job
      • Describing a Cron Job
      • Describing a Table
      • Describing an Endpoint
    • Novel Studio
  • Deployment
    • Github Actions
    • Release Pipeline
    • Deploying to Digital Ocean
    • Deploying to Vercel
    • Deploying to AWS EC2 and RDS
    • Deploying to AWS ECS/Fargate and RDS
Powered by GitBook
On this page
  • Using novel new api
  • Manual Creation
  • Create a new directory in `/app/api`
  • Create a new index.js or index.ts file inside the directory
  • Restart Novel
  • Enhancements
  • Routing Options
  • Using OpenAPI
  • Validation
  • Route Directives

Was this helpful?

  1. Knowledge Base
  2. Novel Server

Creating a REST Endpoint

Last updated 4 months ago

Was this helpful?

Using novel new api

You can use the scaffolding CLI to generate a rest endpoint for you. This is the easiest way to create a REST endpoint.

novel new api

Follow the instructions and your new API should be in /app/api.

Manual Creation

1

Create a new directory in `/app/api`

Create a new directory with the kebab-case format that identifies the function of the endpoint uniquely.

A good format is [subject]-[action]

Examples:

  • user-create

  • post-search

2

Create a new index.js or index.ts file inside the directory

Copy the template file below

/app/api/user-create/index.ts
export default function Handler(instance) {
    instance.post('/your/endpoint', handler);

    async function handler(request, reply) {
        // your logic
    }
}
3

Restart Novel

New endpoints are not picked up by the server hot reload. You will need to restart the novel dev server


Enhancements

Routing Options

Using OpenAPI

You can generate OpenAPI schemas based on Fastify's validation and serialization model

/app/api/user-create/schema.json
{
    "body": {
        "type": "object",
        "properties": {
            "id": {
                "type": "string"
            }
        }
    }
}

This is automatically loaded into your route.

Validation

Because of Fastify's validation model, request input is already controlled on a framework level. If you prefer to validate further, you can make use of zod internally like so:

/app/api/user-create/index.ts
import Accounts from 'novel/models/accounts';
import { z } from 'zod';

export default function Handler(instance) {
    instance.post('/your/endpoint', handler);

    async function handler(request, reply) {
        if (Accounts.z.safeParse(request.body)) {
            // your logic
        }
        if (z.safeParse(request.body)) {
            // your logic
        }
    }
}

Route Directives

You can apply authentication, throttling, and other tools via Novel's route directives.

novel new | Novel
Routing | Novel
Routes | Fastify
Logo
Validation-and-Serialization | Fastify
Logo
TypeScript-first schema validation with static type inferenceGitHub
Logo
Logo
Validation | Novel
Models | Novel
Route Directives | Novel
Logo
Logo
Logo
Logo