Primate Logo Primate
Guides Responses

Serve JSON

Return objects to serve JSON responses. Primate automatically serializes returned objects to JSON.

Objects are JSON-serialized; use response.json() for granular control.

Return object

Primate sets Content-Type: application/json on returned objects and serializes them to JSON strings.

TypeScriptroutes/api.ts
import route from "primate/route";

export default route({
  get() {
    return ({ message: "Hello", data: [1, 2, 3] });
  },
});

Explicit JSON response

Use response.json() for custom options.

TypeScriptroutes/index.ts
import response from "primate/response";
import route from "primate/route";

export default route({
  get() {
    return response.json({ error: "Not found" }, { status: 404 });
  },
});