---
title: HTTP routes
description: 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.

1. **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.

    ```ts
    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);
      }
    }
    ```

2. **Choose the response for failures**

    - **App errors.** Share the mapping in your framework's error handler. See [HTTP statuses and response shape](/docs/reference/errors#kinds-and-statuses).
    - **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

- [Framework callers](/docs/guides/frameworks)
- [oRPC procedures](/docs/guides/orpc)
- [Errors](/docs/reference/errors)
