Setting the file. One moment.
Chapter 12 · Clerk Astro Patterns
Subchapter 12.1
references/api-routes.mdMarkdown2 KBView on GitHub
Also bundled
Evals// src/pages/api/data.ts
import type { APIRoute } from 'astro'
export const GET: APIRoute = async (context) => {
const { userId } = context.locals.auth()
if (!userId) {
return new Response('Unauthorized', { status: 401 })
}
const data = await fetchData(userId)
return new Response(JSON.stringify(data), {
headers: { 'Content-Type': 'application/json' },
})
}export const POST: APIRoute = async (context) => {
const { userId, orgId } = context.locals.auth()
if (!userId) return new Response('Unauthorized', { status: 401 })
if (!orgId) return new Response('No active org', { status: 403 })
const body = await context.request.json()
await saveOrgData(orgId, body)
return new Response(JSON.stringify({ ok: true }), {
headers: { 'Content-Type': 'application/json' },
})
}export const DELETE: APIRoute = async (context) => {
const auth = context.locals.auth()
if (!auth.userId) return new Response('Unauthorized', { status: 401 })
const canDelete = auth.has({ permission: 'org:items:delete' })
if (!canDelete) return new Response('Forbidden', { status: 403 })
await deleteItem(context.params.id!)
return new Response(null, { status: 204 })
}import { clerkClient } from '@clerk/astro/server'
export const GET: APIRoute = async (context) => {
const { userId } = context.locals.auth()
if (!userId) return new Response('Unauthorized', { status: 401 })
const client = clerkClient(context)
const user = await client.users.getUser(userId)
return new Response(JSON.stringify({ name: user.fullName }), {
headers: { 'Content-Type': 'application/json' },
})
}prerender does not applycontext.locals.auth() (not Astro.locals.auth()) in API routes