REST API
Lucent automatically generates RESTful APIs for your collections.
Default Endpoints
For a collection named posts:
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/posts | List all records |
| GET | /api/posts/:id | Get single record |
| POST | /api/posts | Create record |
| PATCH | /api/posts/:id | Update record |
| DELETE | /api/posts/:id | Delete record |
Query Parameters
Pagination
bash
GET /api/posts?page=2&perPage=20Filtering
bash
GET /api/posts?filter=status:published
GET /api/posts?filter=author:user123
GET /api/posts?filter=publishedAt:>2024-01-01Sorting
bash
GET /api/posts?sort=createdAt:desc
GET /api/posts?sort=title:ascField Selection
bash
GET /api/posts?fields=id,title,slugRelations
bash
GET /api/posts?include=author,tagsSearch
bash
GET /api/posts?search=keywordResponse 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],
});