Skip to content

REST API

Lucent automatically generates RESTful APIs for your collections.

Default Endpoints

For a collection named posts:

MethodEndpointDescription
GET/api/postsList all records
GET/api/posts/:idGet single record
POST/api/postsCreate record
PATCH/api/posts/:idUpdate record
DELETE/api/posts/:idDelete record

Query Parameters

Pagination

bash
GET /api/posts?page=2&perPage=20

Filtering

bash
GET /api/posts?filter=status:published
GET /api/posts?filter=author:user123
GET /api/posts?filter=publishedAt:>2024-01-01

Sorting

bash
GET /api/posts?sort=createdAt:desc
GET /api/posts?sort=title:asc

Field Selection

bash
GET /api/posts?fields=id,title,slug

Relations

bash
GET /api/posts?include=author,tags
bash
GET /api/posts?search=keyword

Response Format

json
{
  "data": [...],
  "meta": {
    "total": 100,
    "page": 1,
    "perPage": 20,
    "totalPages": 5
  }
}

Custom Endpoints

Create custom API endpoints:

typescript
// src/endpoints/posts.ts
import { defineEndpoint } from "@lucent/core";

export const postsEndpoint = defineEndpoint({
  path: "/posts/published",
  method: "GET",
  handler: async ({ db, query }) => {
    const posts = await db.query("posts").where("published", true).sort("createdAt", "desc").limit(10).execute();

    return posts;
  },
});

Register in config:

typescript
export default defineConfig({
  endpoints: [postsEndpoint],
});

Released under the MIT License.