Endpoints and JSON

A .ts file with no template of the same name is an endpoint: it serves its own URL from code. Use one for JSON, feeds, webhooks, event streams and anything else that isn't an HTML page.

// pb_pages/feeds/latest.ts
module.exports = {
  get(ctx) {
    const posts = ctx.app.findRecordsByFilter('posts', 'published = true', '-created', 20)
    return ctx.res.json(posts.map((p) => ({ id: p.id, title: p.getString('title') })))
  },
}

A request to /feeds/latest returns:

[{ "id": "abc123", "title": "Hello" }]

How it works

  • The URL comes from the file's path, as for pages: feeds/latest.ts serves /feeds/latest, feeds/index.ts serves /feeds, products/[id].ts serves /products/123.
  • An endpoint has the same exports as page code: load, get, post, put, patch, delete.
  • There is no template, so a handler must send a response. Returning without one is a 500.
  • A method with no handler answers 405, GET included, with an Allow header listing the methods it has.
  • An endpoint and a page can't claim the same URL: feed.ts and feed/index.ets is an error at startup.
  • Middleware runs for endpoints too; layouts don't.
  • Handlers can be async. The response is sent once the handler settles.

Errors

An error thrown in an endpoint (ctx.httpError() or any other) answers with JSON: { "status": 404, "message": "..." }. When the request's Accept header puts text/html first, as a browser does when someone opens the URL, the error is your +error.ets page instead.

// pb_pages/products/[id].ts
module.exports = {
  get(ctx) {
    const product = ctx.app.findFirstRecordByFilter('products', 'id = {:id}', { id: ctx.params.id })
    if (!product) throw ctx.httpError(404, 'No such product')
    return ctx.res.json({ id: product.id, name: product.getString('name') })
  },
}

Async handlers

// pb_pages/stats.ts
module.exports = {
  async get(ctx) {
    const orders = await ctx.app.countRecords.async('orders')
    return ctx.res.json({ orders })
  },
}

An async handler still has to send a response. Returning a value without calling ctx.res.json (or another ctx.res method) is a 500, as it is for a sync handler.

Sending responses

Call Sends
ctx.res.json(data) / ctx.res.json(status, data) JSON
ctx.res.text(text) / ctx.res.text(status, text) Plain text
ctx.res.html(html) / ctx.res.html(status, html) HTML as-is, without layouts
ctx.res.file(path) A file from pb_pages
ctx.res.redirect(location) A redirect
ctx.res.sse() A text/event-stream

See res.

JSON from a page

JSON is only sent through ctx.res.json (and an endpoint's errors). Template output is never parsed as JSON, even if it looks like JSON. A page's code can still answer with JSON for one method:

// pb_pages/products/[id].ts, beside products/[id].ets
module.exports = {
  post(ctx) {
    const product = ctx.app.findRecordById('products', ctx.params.id)
    return ctx.res.json({ id: product.id, name: product.getString('name') })
  },
}

Keep /api/ free

/api/ belongs to PocketBase. Pages never see it, and a pb_pages/api/ folder is an error at startup. Put your endpoints somewhere else, such as feeds/, xapi/ or hooks/.