Skip to content
Logic SDK
Esc
↑↓navigate↵open⌘Jpreview
On this page

HTTP routes

Run a function from a route and return app errors as HTTP responses.

toResponse maps an app error to a JSON response and an HTTP status.

Bind the scope and run the function

Build a caller from the request. Run a function with authorize, using the parsed request body as input.

import { toResponse } from "logic-sdk/fetch";

export async function POST(request: Request) {
  try {
    const caller = registry.as(await scopeFrom(request));
    const booking = await caller.run(bookings.confirm, await request.json());
    return Response.json(booking);
  } catch (error) {
    return toResponse(error);
  }
}

Choose the response for failures

  • App errors. Share the mapping in your framework’s error handler. See HTTP statuses and response shape.
  • Unexpected errors. toResponse throws them again. Configure your framework to log them and return a generic 500.
  • Signed out. Use the signed-out scope on a public route. A function policy can reject it with 403. On a session-only route, throw unauthenticated() from logic-sdk before you bind the caller.

A staff request with a valid booking ID returns the confirmed booking.

See also

Was this page helpful?