Skip to content

Interactive Documentation Features

This guide walks through building a REST API while showcasing the interactive components available in Starlight. Components are imported from @astrojs/starlight/components and used directly in your content.

  1. Set up your project

    Create a new directory and initialize your API project. We will use a simple Node.js setup with Express.

    Terminal window
    mkdir my-api && cd my-api
    npm init -y
  2. Install dependencies

    Install Express and a few helpful utilities for building a production-ready API.

    bash npm install express cors helmet morgan
  3. Create the entry point

    Create an index.js file with the basic server setup:

    const express = require("express");
    const cors = require("cors");
    const helmet = require("helmet");
    const morgan = require("morgan");
    const app = express();
    app.use(cors());
    app.use(helmet());
    app.use(morgan("combined"));
    app.use(express.json());
    app.listen(3000, () => {
    console.log("API running on http://localhost:3000");
    });
  4. Define your first route

    Add a health check endpoint so monitoring tools can verify the API is running.

    app.get("/health", (req, res) => {
    res.json({ status: "ok", uptime: process.uptime() });
    });
  5. Test the API

    Start the server and confirm everything works:

    Terminal window
    node index.js
    curl http://localhost:3000/health

Routing

Define endpoints that map HTTP methods and paths to handler functions. Express supports GET, POST, PUT, PATCH, and DELETE out of the box.

Middleware

Functions that run before your route handler. Use them for authentication, logging, input validation, and error handling.

Authentication

Protect your endpoints with API keys, JWT tokens, or OAuth. Always validate credentials in middleware, not in individual route handlers.

Error Handling

Return consistent error responses with appropriate HTTP status codes. A global error handler catches anything your routes miss.

Every endpoint should return a consistent JSON structure. Here is the pattern used throughout this API:

{
"status": "success",
"data": {
"id": 42,
"name": "Example Resource",
"created_at": "2026-03-17T10:00:00Z"
}
}

When documenting your API, use badges to communicate the stability of each endpoint:

EndpointMethodStatus
/v1/usersGETStable
/v1/users/:idGETStable
/v1/usersPOSTStable
/v1/users/:id/avatarPUTBeta
/v1/searchGETExperimental
/v1/webhooksPOSTNew

Starlight with MDX supports embedding framework components directly. This counter is a Solid.js component hydrated on the client:

0

Start from a different value by passing the initial attribute:

10

This works by importing an Astro wrapper around a Solid.js component with client:visible hydration.