Custom APIs let you expose a small, deliberate set of routes on top of SurrealDB. Instead of handing every caller a generic query channel, you can define named endpoints that return a familiar HTTP-shaped result.
That pattern pairs well with tightening access such as varying querying timeouts for free or entry-level users on an app. To accomplish this, you can combine DEFINE API with capabilities or server environment variables so certain users only see the APIs you designed.
This page and the middleware page build the API of a small guesthouse. Guests make bookings, look up rooms and fetch room photos, and each section adds a route to the same API.
Where endpoints live
Each definition maps to a path under /api/:namespace/:database/..., followed by the path you gave in the statement. As such, an endpoint path like bookings in namespace guesthouse and database main becomes /api/guesthouse/main/bookings over HTTP.
Reading the incoming request
Inside the handler you can use the built-in $request value. This parameter holds the method, body, headers, query, params (from your path pattern), and context you or middleware may have set.
A minimal endpoint
The first route is /bookings. Each method gets its own FOR clause and its own handler: get lists the bookings, and post creates a booking from $request.body and answers with status 201 and a header. The get handler also lists the built-in api::timeout middleware, which stops a slow listing after one second.
The handler returns an object with any of the fields status, body and headers. You can test the endpoint from SurrealQL without needing to deploy by calling the api::invoke function. It takes the path, and optionally an object with the request's method, body, headers and query. The method is get when the object leaves it out.
DEFINE API "/bookings"
FOR get
MIDDLEWARE
api::timeout(1s)
THEN {
{
status: 200,
body: SELECT guest, room, nights FROM booking ORDER BY guest
};
}
FOR post
THEN {
LET $booking = CREATE ONLY booking CONTENT $request.body;
{
status: 201,
body: $booking.{ guest, room, nights },
headers: { 'cache-control': 'no-store' }
};
};
api::invoke("/bookings", {
method: "post",
body: { guest: "Ana", room: room:garden, nights: 3 }
}).status;
//- 201
api::invoke("/bookings", {
method: "post",
body: { guest: "Ben", room: room:attic, nights: 1 }
}).status;
//- 201
api::invoke("/bookings").body;
//- [{ guest: 'Ana', nights: 3, room: room:garden }, { guest: 'Ben', nights: 1, room: room:attic }]
-- No definition for this path
api::invoke("/booking").status;
//- 404
-- The path has no handler for this method
api::invoke("/bookings", { method: "delete" }).status;
//- 404Without .status or .body, api::invoke returns the whole response: the status, body and headers from the handler, and a request_id that SurrealDB also adds to the headers as x-surreal-request-id.
Path patterns: one segment or the rest of the URL
The path string in DEFINE API can be static, like /bookings, or it can capture pieces of the URL.
A segment like "/rooms/:name" binds one path component, and the value in that slot shows up on $request.params. The handler below uses it to look up a room. A room that does not exist gets a 404 from the handler itself, with a message, while a path with an extra segment matches no route at all:
CREATE room:garden SET beds = 2, price = 120;
CREATE room:attic SET beds = 1, price = 80;
DEFINE API "/rooms/:name" FOR get THEN {
LET $room = type::record("room", $request.params.name);
IF $room.exists() {
{ status: 200, body: $room.{ beds, price } }
} ELSE {
{ status: 404, body: "No room called " + $request.params.name }
}
};
api::invoke("/rooms/garden").body;
//- { beds: 2, price: 120 }
api::invoke("/rooms/cellar").body;
//- 'No room called cellar'
api::invoke("/rooms/garden/photos").body;
//- 'Not found'The last call does not reach the handler, because :name only covers a single segment. Extra slashes mean that no route matches, which api::invoke reports as status 404 with the body 'Not found'.
To accept any number of trailing segments, use * instead of :. The capture then holds the rest of the path as an array of segments, so a photo route can serve both a flat file name and a nested path:
DEFINE API "/photos/*path" FOR get THEN {
{ status: 200, body: { file: $request.params.path } }
};
api::invoke("/photos/garden.jpg").body;
//- { file: ['garden.jpg'] }
api::invoke("/photos/garden/2026/window.jpg").body;
//- { file: ['garden', '2026', 'window.jpg'] }Middleware
Built-in helpers such as api::timeout sit in the MIDDLEWARE list before your THEN block. The next page adds middleware of its own to the guesthouse API.
Where to read more
DEFINE API- full statement reference, includingPERMISSIONS.API functions -
api::invokeand related helpers.Custom functions - how
fn::functions fit into larger designs.