- Guides
- Recipes
Get a record
Fetch a single record by its AT URI.
Trigger: xrpc.query:<nsid>. Needs: happyview-db.
local db = require("happyview.db")
function handle(input, ctx) if not input.uri then return { error = "UriRequired", message = "uri parameter is required" } end
local row = db.get(input.uri) if not row then return { error = "RecordNotFound", message = "no record at that uri" } end
return { record = row }endJavaScript
Section titled “JavaScript”import db from "happyview.db";
export default async function handle(input, ctx) { if (!input.uri) { return { error: "UriRequired", message: "uri parameter is required" }; }
const row = await db.get(input.uri); if (!row) { return { error: "RecordNotFound", message: "no record at that uri" }; }
return { record: row };}TypeScript
Section titled “TypeScript”import db from "happyview.db";
interface Input { uri?: string;}
export default async function handle(input: Input) { if (!input.uri) { return { error: "UriRequired", message: "uri parameter is required" }; }
const row = await db.get(input.uri); if (!row) { return { error: "RecordNotFound", message: "no record at that uri" }; }
return { record: row };}How it works
Section titled “How it works”- Check that the
uriquery parameter is present, and return a structured error if not. - Look the record up with
db.get. It returns an envelope,{ uri, did, collection, rkey, cid, indexed_at, record }, or nothing. The record body is the envelope’srecordfield. In JavaScript and TypeScript,db.getreturns a promise, so it is awaited. - Return the envelope wrapped in an object.
The script returns structured errors instead of throwing, so the client gets a body it can handle rather than a 500. A returned error is a 400 only when its name is declared under defs.main.errors in the lexicon:
"errors": [{ "name": "UriRequired" }, { "name": "RecordNotFound" }]An undeclared error is sent as an ordinary 200 response body.
GET /xrpc/xyz.statusphere.getRecord?uri=at://did:plc:abc/xyz.statusphere.status/3k2a© HappyProto

