Astro Quickstart
You will learn the following:
- Install
@clerk/astro
- Set up your environment keys to test your app locally
- Add TypeScript declarations
- Add the
clerk()
integration to your application - Use Clerk middleware to implement route-specific authentication
- Create a header with Clerk components for users to sign in and out
Before you start
Example repository
Clerk's Astro SDK provides a set of components, hooks, and stores that make it easy to build authentication and user management features in your Astro app.
Install @clerk/astro
Add Clerk's Astro SDK to your project:
Update astro.config.mjs
To configure Clerk in your Astro application, you will need to update your astro.config.mjs
.
- Add the Clerk integration to the
integrations
list. - Install an SSR adapter. For this quickstart we chose theÂ
@astrojs/node
 adapter. You can use any Node based adapter you wish. - SetÂ
output
 toÂserver
. This is required when deploying to a host supporting SSR.
Add middleware to your application
clerkMiddleware()
 grants you access to user authentication state throughout your application, on any route or page. It also allows you to protect specific routes from unauthenticated users. To add clerkMiddleware()
 to your application, follow these steps:
-
Create a
middleware.ts
file inside yoursrc/
directory. -
In your
middleware.ts
file, export anonRequest
constant and assign the result of theclerkMiddleware
function to it. -
By default,
clerkMiddleware()
will not protect any routes. All routes are public and you must opt-in to protection for routes. See theclerkMiddleware()
reference to learn how to require authentication for specific routes.
Add TypeScript declarations
Update the env.d.ts
file in your src/
directory to add type definitions for the locals
added by the middleware.
Add components to your app
Clerk offers components that allow you to protect your pages. These components are used to control the visibility of your pages based on the user's authentication state.
Create your first user
Run your project with the following command:
Now visit your app's homepage at http://localhost:4321
. Sign up to create your first user.
Protect routes using Clerk Middleware
Learn how to protect specific routes from unauthenticated users.
Read session and user data
Learn how to use Clerk's stores and helpers to access the active session and user data in your Astro application.
Clerk + Astro Quickstart Repo
The official companion repo for Clerk's Astro Quickstart.
Feedback
Last updated on