Skip to content

Adding a BFF Endpoint

How to add a new endpoint to the Mobile BFF Worker.

1. Add the endpoint in the BFF

Edit src/workers/mobile-bff/index.ts:

typescript
app.get("/api/mobile/your-endpoint", async (c) => {
  const sql = getOrgSql(c);  // Gets the org's PostgreSQL connection
  const userId = c.req.header("X-User-Id");  // Current user

  const results = await sql`
    SELECT * FROM your_table
    WHERE some_condition = ${someValue}
  `;

  return c.json({ results });
});

Key points:

  • getOrgSql(c) returns a Neon tagged template SQL function connected to the active org's database
  • c.req.header("X-User-Id") and c.req.header("X-Org-Id") are injected by the gateway
  • Use tagged template syntax for queries: sql`SELECT * FROM x WHERE id = ${id}`
  • All /api/mobile/* routes are automatically authenticated and org-scoped by the gateway

2. Add to G2ApiClient

Edit YouthSportMate/youth-sports-mobile/src/services/G2ApiClient.js:

javascript
async getYourData(params) {
  return this.request('/api/mobile/your-endpoint');
}

3. Add to ApiAdapter (if existing screens need it)

Edit YouthSportMate/youth-sports-mobile/src/services/ApiAdapter.js:

javascript
async getYourData(params) {
  const { results } = await g2ApiClient.getYourData(params);
  return results || [];
}

4. Type-check and deploy

bash
cd g2-elite-backend
npx tsc --noEmit
npx wrangler deploy --config wrangler.mobile-bff.toml

Patterns

  • List endpoints return { items: [...] } — adapter unwraps to plain array
  • Detail endpoints return { item: {...} } — adapter unwraps to plain object
  • Create endpoints accept POST body, return { item: {...} }
  • Update endpoints accept PATCH body, return { item: {...} }
  • Error responses always return { error: "message" } with appropriate HTTP status

Internal Documentation — Do Not Share