JavaScript Quickstart
You will learn the following:
- Add the JavaScript SDK to your JavaScript app
- Use Clerk components to allow users to sign in or out
To add the JavaScript SDK to your JavaScript app, you have two options:
- Install the package using a package manager, like
npm
. - Use the
<script>
tag to load the ClerkJS package from our CDN.
Use the following tabs to choose your preferred method.
Set up a JavaScript app using Vite
To install Clerk's JavaScript SDK, you need to use a bundler like Vite or Webpack.
For this tutorial, run the following commands to create a JavaScript app using Vite. From the prompts, choose the Vanilla framework, and then choose the JavaScript variant.
Install @clerk/clerk-js
Run the following command to add the JavaScript SDK to your project:
Set your Clerk API keys
It's recommended to use environment variables to store your Clerk publishable key. In JavaScript projects, you can add these values in an .env
file and load them into your app using a package like dotenv
. For Vite projects, environment variables in an .env
file at the project root are automatically accessible through the import.meta.env
object.
In your main.js
file, import the publishable key using Vite's import.meta.env
object.
Initialize Clerk
To initialize Clerk, import the Clerk
class and instantiate it with your Clerk publishable key. Then, call the load()
method, as shown in the following example:
Add Clerk components to your app
Clerk's prebuilt components are the easiest way to add authentication and user management to your app. They come styled out-of-the-box and are customizable to fit your app's design.
To get started, add the following components to your app:
<SignIn />
: Renders a user interface for signing in.<UserButton />
: Shows the signed-in user's avatar. Selecting it opens a dropdown menu with account management options.
Your main.js
file should look something like this:
And your index.html
file should look something like this:
Create your first user
Run your project with the following command:
Now visit your app's homepage at http://localhost:5173
. Sign up to create your first user.
Add the SDK using a <script>
tag
This <script>
tag will load Clerk's JavaScript SDK from our CDN and initialize it with your Clerk Publishable Key and Frontend API URL. It should be placed before any other <script>
tags that use the JavaScript SDK.
- In the Clerk Dashboard, navigate to the API Keys page.
- In the Quick Copy section, select JavaScript from the dropdown menu.
- Copy the
<script>
tag and paste it into your HTML file, as shown in the following example:
Listen for the load
event
Below the <script
> tag that initializes the SDK, create another <script>
tag that listens for the load
event and call Clerk's load()
method to load the SDK, as shown in the following example:
Allow users to sign in or out
Clerk's prebuilt components are the easiest way to add authentication and user management to your app. They come styled out-of-the-box and are customizable to fit your app's design.
To get started, you will use:
<SignIn />
: renders a user interface for signing in.<UserButton />
: Shows the signed-in user's avatar. Selecting it opens a dropdown menu with account management options.
Clerk class reference
Learn more about the Clerk
class and how to use it.
Prebuilt components
Learn more about Clerk's suite of components that let you quickly add authentication to your app.
Customization & localization
Learn how to customize and localize Clerk components.
JavaScript SDK Reference
Learn more about additional JavaScript methods.
Feedback
Last updated on