Skip to main content

Astro Quickstart

Use this prebuilt prompt to get started faster.

Create a new Astro app

If you don't already have an Astro app, run the following commands to create a new one.

npm create astro@latest clerk-astro
cd clerk-astro
npm install
pnpm create astro clerk-astro
cd clerk-astro
pnpm install
yarn create astro clerk-astro
cd clerk-astro
yarn install
bunx create-astro clerk-astro
cd clerk-astro
bun install

Install @clerk/astro

The Clerk Astro SDKAstro Icon gives you access to a set of components, hooks, and stores to make user authentication easier.

Run the following command to install the SDK:

npm install @clerk/astro
pnpm add @clerk/astro
yarn add @clerk/astro
bun add @clerk/astro
.env
PUBLIC_CLERK_PUBLISHABLE_KEY=YOUR_PUBLISHABLE_KEY
CLERK_SECRET_KEY=YOUR_SECRET_KEY

Update astro.config.mjs

To configure Clerk in your Astro app, you will need to update your astro.config.mjs.

astro.config.mjs
import { defineConfig } from 'astro/config'
import node from '@astrojs/node'
import clerk from '@clerk/astro'

export default defineConfig({
  integrations: [clerk()],
  adapter: node({ mode: 'standalone' }),
  output: 'server',
})

Add clerkMiddleware() to your app

clerkMiddleware()Astro Icon grants you access to user authentication state throughout your app. It also allows you to protect specific routes from unauthenticated users. To add clerkMiddleware() to your app, follow these steps:

  1. Create a middleware.ts file.
    • If you're using the /src directory, create middleware.ts in the /src directory.
    • If you're not using the /src directory, create middleware.ts in the root directory alongside .env.
  2. In your middleware.ts file, export an onRequest constant and assign the result of the clerkMiddleware() function to it.
    src/middleware.ts
    import { clerkMiddleware } from '@clerk/astro/server'
    
    export const onRequest = clerkMiddleware()
  3. By default, clerkMiddleware() will not protect any routes. All routes are public and you must opt-in to protection for routes. See the clerkMiddleware() referenceAstro Icon to learn how to require authentication for specific routes.

Create a header with Clerk components

You can control which content signed-in and signed-out users can see with Clerk's prebuilt control components. The following example creates a header using the following components:

src/layouts/Layout.astro
---
import { Show, UserButton, SignInButton, SignUpButton } from '@clerk/astro/components'
---

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width" />
    <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
    <meta name="generator" content={Astro.generator} />
    <title>Astro Basics</title>
  </head>
  <body>
    <header>
      <Show when="signed-out">
        <SignInButton mode="modal" />
        <SignUpButton mode="modal" />
      </Show>
      <Show when="signed-in">
        <UserButton />
      </Show>
    </header>
    <slot />
  </body>
</html>

<style>
  html,
  body {
    margin: 0;
    width: 100%;
    height: 100%;
  }
</style>

Then, use the layout on your homepage:

src/pages/index.astro
---
import Layout from '../layouts/Layout.astro'
import { Show } from '@clerk/astro/components'
---

<Layout title="Clerk + Astro">
  <Show when="signed-out">
    <p>Sign in to try Clerk out!</p>
  </Show>
  <Show when="signed-in">
    <p>You are signed in!</p>
  </Show>
</Layout>

Run your project

Run your project with the following command:

npm run dev
pnpm run dev
yarn dev
bun run dev

Create your first user

  1. Visit your app's homepage at http://localhost:4321.
  2. Select "Sign up" on the page and authenticate to create your first user.

Next steps

Explore the most relevant next steps for your SDK using the following guides.

Prebuilt components

Learn how to add Clerk's prebuilt authentication and user-management UI to your app.

Build custom flows

Learn how to build custom user interfaces entirely from scratch using the Clerk API.

Protect content and read user data

Learn how to use Clerk's helpers to protect content and read user data in your app.

Protect routes using clerkMiddleware()

Learn how to protect specific routes from unauthenticated users.

More to explore

Explore additional Clerk features that help you build, manage, and grow your application.

  • Organizations - Organizations are shared accounts that let teams collaborate, manage members and roles, and control access to shared resources.
  • Billing - Billing enables you to manage subscriptions, free trials, payments, plans, and billing-related webhook events for B2C and B2B applications.
  • Waitlist - Waitlist lets you collect signups and control access to new products or features before launch through a simple, integrated workflow.

Feedback

What did you think of this content?

Last updated on