agentleFS
buildpad-aibuildpad-ai/skills/create-api-route/SKILL.md

create-api-route

Skill1 starsChanged 4 months agoMITView on GitHub

Generate a server-side Next.js API route for Supabase auth (login, logout, callback, session). These routes manage Supabase SSR cookies and run server-side. Do NOT use this skill to create proxy routes for DaaS data — the UI calls DaaS directly using Authorization Bearer tokens instead. Use when the user needs a new auth endpoint or server-side Supabase route.

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 1 is distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    When an aggregate parameter is present, the GET handler returns computed values instead of individual items.

    line 71
  2. Distinctive:

    All routes must check auth:

    line 77

≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Create API Route

Generate a server-side Supabase auth route. These routes manage HTTP-only cookies for Supabase SSR sessions.

Important: Only create auth routes here. Data routes (items, fields, files, etc.) are NOT needed — the UI calls DaaS directly using Authorization: Bearer <supabase-jwt>. CORS is handled by DaaS via CORS_ORIGINS env var.

Auth Routes Only

app/api/auth/
├── login/route.ts      # POST — Supabase signInWithPassword
├── logout/route.ts     # POST — Supabase signOut
├── callback/route.ts   # GET  — OAuth/magic-link code exchange
└── user/route.ts       # GET  — Current user from server-side session

Route Template

import { NextRequest, NextResponse } from "next/server";
import { createClient } from "@/lib/supabase/server";

type RouteParams = { params: Promise<{ collection: string }> };

export async function GET(request: NextRequest, { params }: RouteParams) {
  const { collection } = await params;
  const supabase = await createClient();
  const { searchParams } = new URL(request.url);

  // Parse query parameters
  const limit = parseInt(searchParams.get("limit") || "100");
  const offset = parseInt(searchParams.get("offset") || "0");
  const sort = searchParams.get("sort");
  const filter = searchParams.get("filter");

  let query = supabase.from(collection).select("*", { count: "exact" });
  query = query.range(offset, offset + limit - 1);
  if (sort)
    query = query.order(sort.replace("-", ""), {
      ascending: !sort.startsWith("-"),
    });

  const { data, error, count } = await query;
  if (error)
    return NextResponse.json(
      { errors: [{ message: error.message }] },
      { status: 500 },
    );
  return NextResponse.json({
    data,
    meta: { total_count: count, filter_count: count },
  });
}

Query Parameters

  • fields — Comma-separated field names or *
  • filter — JSON filter object { "status": { "_eq": "published" } }
  • sort — Sort field(s) with - prefix for descending
  • limit, offset — Pagination
  • aggregate — Aggregate functions: aggregate[count]=id or aggregate={"count":["id"]}
  • groupBy — Group aggregate results by field(s)

When an aggregate parameter is present, the GET handler returns computed values instead of individual items. Supported operations: count, countDistinct, countAll, sum, sumDistinct, avg, avgDistinct, min, max. Response format: { data: [{ count: { id: 42 } }] } (no pagination meta).

⚠️ Field Name Verification: Before using any field name in sort, fields, or filter parameters, verify it exists in the DaaS schema using mcp_daas_schema or mcp_daas_fields. Using a non-existent field (e.g., sort=-date_created when the actual column is created_at) causes DaaS to return a 500 error with no helpful message. Never assume field names — always check the schema first.

Authentication

All routes must check auth:

const supabase = await createClient();
const {
  data: { user },
} = await supabase.auth.getUser();
if (!user)
  return NextResponse.json(
    { errors: [{ message: "Unauthorized" }] },
    { status: 401 },
  );

Input Validation (Zod)

import { z } from "zod";
const schema = z.object({
  name: z.string().min(1).max(255),
  status: z.enum(["draft", "published"]),
});
const validated = schema.parse(body);

Required Tests

Create tests/api/[collection].spec.ts with:

  • GET all items (200) + query parameters
  • POST create with valid data (201) + invalid data (400)
  • GET single item (200) + non-existent (404)
  • PATCH update (200) + non-existent (404)
  • DELETE item (200/204) + non-existent (404)

References

  • API route patterns
  • API authorization

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.