Astro Quickstart
Before you start
Example repository
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 installpnpm create astro clerk-astro
cd clerk-astro
pnpm installyarn create astro clerk-astro
cd clerk-astro
yarn installbunx create-astro clerk-astro
cd clerk-astro
bun installInstall @clerk/astro
The Clerk Astro SDK
Run the following command to install the SDK:
npm install @clerk/astropnpm add @clerk/astroyarn add @clerk/astrobun add @clerk/astroAdd the following keys to your .env file. These keys can always be retrieved from the API keys page in the Clerk Dashboard.
- In the Clerk Dashboard, navigate to the API keys page.
- In the Quick Copy section, copy your Clerk and .
- Paste your keys into your
.envfile.
The final result should resemble the following:
PUBLIC_CLERK_PUBLISHABLE_KEY=YOUR_PUBLISHABLE_KEY
CLERK_SECRET_KEY=YOUR_SECRET_KEYUpdate astro.config.mjs
To configure Clerk in your Astro app, you will need to update your astro.config.mjs.
- Add the
clerk()integration to theintegrationslist. For a list of available options, see the integration reference .Astro Icon - Install an SSR adapter. This quickstart uses the
@astrojs/nodeadapter. - Set
outputtoserver. This is required when deploying to a host supporting SSR.
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()clerkMiddleware() to your app, follow these steps:
- Create a
middleware.tsfile.- If you're using the
/srcdirectory, createmiddleware.tsin the/srcdirectory. - If you're not using the
/srcdirectory, createmiddleware.tsin the root directory alongside.env.
- If you're using the
- In your
middleware.tsfile, export anonRequestconstant and assign the result of theclerkMiddleware()function to it.src /middleware.ts import { clerkMiddleware } from '@clerk/astro/server' export const onRequest = clerkMiddleware() - By default,
clerkMiddleware()will not protect any routes. All routes are public and you must opt-in to protection for routes. See the clerkMiddleware() reference to learn how to require authentication for specific routes.Astro Icon
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:
- <Show when="signed-in">: Children of this component can only be seen while signed in.
- <Show when="signed-out">: Children of this component can only be seen while signed out.
- <UserButton />: Shows the signed-in user's avatar. Selecting it opens a dropdown menu with account management options.
- <SignInButton />: An unstyled component that links to the sign-in page. In this example, since no props or environment variables are set for the sign-in URL, this component links to the Account Portal sign-in page.
- <SignUpButton />: An unstyled component that links to the sign-up page. In this example, since no props or environment variables are set for the sign-up URL, this component links to the Account Portal sign-up page.
---
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:
---
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 devpnpm run devyarn devbun run devCreate your first user
- Visit your app's homepage at http://localhost:4321.
- Select "Sign up" on the page and authenticate to create your first user.
Next steps
Learn more about Clerk components, how to customize them, and how to use Clerk's stores to access user data using the following guides.
Prebuilt components
Learn how to quickly add authentication to your app using Clerk's suite of components.
Customization & localization
Learn how to customize and localize Clerk components.
Protect routes using clerkMiddleware()
Learn how to protect specific routes from unauthenticated users.
Protect content and read user data
Learn how to use Clerk's stores and helpers to protect content and read user data in your Astro app.
Use Clerk with Astro and React
Learn how to set up your Astro app to be integrated with React.
Get started with Organizations
Learn how to create and manage Organizations in your Astro app.
Clerk Astro SDK Reference
Learn about the Clerk Astro SDK and how to integrate it into your app.
Feedback
Last updated on