Skip to main content
Docs

Clerk Astro SDK

The Clerk Astro SDK gives you access to prebuilt components, stores, and helpers to make user authentication easier. Refer to the quickstart guide to get started.

Integration

To configure Clerk with Astro, you must pass the clerk() integration to the integrations array in your astro.config.mjs file. See the quickstart for more information on configuring the integration.

updateClerkOptions()

The updateClerkOptions() function is used to update Clerk's options at runtime. It can be called at any time after Clerk has been initialized. See the reference documentation for more information.

Client-side helpers

The Astro SDK provides stores that give you access to the Clerk object and helper methods for authentication flows.

Server-side helpers

The following references show how to integrate Clerk features into your Astro app on the server-side.

Locals

The Astro SDK provides access to Clerk's authentication data through Astro's locals object. The following references show how to access authentication data in server-side code:

clerkMiddleware()

The clerkMiddleware() helper integrates Clerk authentication and authorization into your Astro application through middleware. You can learn more here.

clerkClient()

Clerk's JavaScript Backend SDK provides access to Backend API resources and low-level authentication utilities for JavaScript environments. For example, to retrieve a list of all users in your application, you can use the users.getUserList() method from the JavaScript Backend SDK instead of manually making a fetch request to the https://api.clerk.com/v1/users endpoint.

All resource operations are mounted as sub-APIs on the clerkClient object. See the reference documentation for more information.

Example: Use clerkClient to get a user's information

The following example uses clerkClient to get information about the currently signed-in user. If the user is authenticated, their userId is passed to clerkClient.users.getUser() to get the current user's User object. If not authenticated, the user is redirected to the sign-in page.

import { clerkClient } from '@clerk/astro/server'

export async function GET(context) {
  const { userId, redirectToSignIn } = context.locals.auth()

  if (!userId) {
    return redirectToSignIn()
  }

  const user = await clerkClient(context).users.getUser(userId)

  return new Response(JSON.stringify({ user }))
}

Feedback

What did you think of this content?

Last updated on