FASTANGANGULAR + FASTIFY0.1Server render
FASTANG / GUIDE

Fastify APIs and validation

Register typed endpoints with runtime schemas and use native Fastify requests and replies.

Names and methods

src/server/api/health.get.ts becomes GET /api/health. Supported suffixes are get, post, put, patch, delete, head and options. [name] creates a dynamic parameter; files without a method remain helpers and are not registered.

Create a POST endpoint

Add this file to the starter. The TypeScript type describes the handler body; the schema validates it at runtime and defines the serialized response.

src/server/api/messages.post.ts
import { defineApiRoute } from '@fastang/core';

export default defineApiRoute<{ Body: { text: string } }>({
  schema: {
    body: {
      type: 'object', required: ['text'], additionalProperties: false,
      properties: { text: { type: 'string', minLength: 1, maxLength: 120 } },
    },
    response: {
      201: {
        type: 'object', required: ['message'],
        properties: { message: { type: 'string' } },
      },
    },
  },
  handler: async (request, reply) => reply.code(201).send({ message: request.body.text.trim() }),
});

Try the response

bash
curl -i -X POST http://localhost:3278/api/messages \
  -H 'content-type: application/json' \
  -d '{"text":"Hello"}'

The response is HTTP 201 with {"message":"Hello"}. A missing text, an empty string or a value longer than 120 characters returns HTTP 400. TypeScript types alone do not validate external requests.

Use Fastify

The handler receives native request and reply: read parameters and headers or set the status. Register shared plugins and hooks in src/server/plugins.ts. The initial body limit is 1 MiB. Internal errors do not expose stacks to the client; unknown API endpoints return JSON with HTTP 404.

Do not import src/server modules into Angular components. Share types through a module without server dependencies and use type-only imports where possible. This alpha does not generate a route-typed API client yet.

Continue with resolvers and SSR. Read more about Fastify validation and serialization.