JavaScript Quickstart
You will learn the following:
- Add the ClerkJS SDK to your JavaScript application
- Use Clerk components to allow users to sign in or out
To add the ClerkJS SDK to your JavaScript application, you have two options:
- Install the package using a package manager, like
npm
,yarn
, orpnpm
. - Use the
<script>
tag to load the ClerkJS package from our CDN.
Select your preferred method below to get started.
Set up a JavaScript application using Vite
To install the ClerkJS SDK, you will need to use a bundler like Vite or Webpack to use this method.
For the sake of this guide, scaffold your new JavaScript application using Vite. From the prompts, choose the Vanilla framework, and then choose the JavaScript variant.
Install @clerk/clerk-js
At the root of your project, install the ClerkJS package using your package manager of choice:
Set environment variables
It's recommended to use environment variables to store your Clerk Publishable Key. A common way to use environment variables in a JavaScript project is by putting the values in an .env
file and then loading them into your application using a package like dotenv
. However, when using Vite, environment variables stored in an .env
file at the project root are automatically exposed through the import.meta.env
object.
- Create an
.env
file at the root of your project, and add your Clerk Publishable Key. If you're signed into Clerk, the.env
snippet below will contain your key. Otherwise, you can copy it from the Clerk Dashboard by navigating to the API Keys page.
- In your
main.js
file, import the publishable key using Vite'simport.meta.env
object.
Initialize ClerkJS
To initialize the ClerkJS SDK, import the Clerk
class and instantiate it with your Publishable Key. Then, call the load()
method to initialize ClerkJS.
Allow users to sign in and out
Clerk's prebuilt components are the easiest way to add authentication and user management to your application. They come styled out of the box and are customizable to fit your application's design.
To get started, you will use:
<SignIn />
: renders a user interface for signing in.<UserButton />
: shows the avatar from the account the user is signed in with. When clicked, it opens a dropdown menu with options to sign out.
Your main.js
file should look something like this:
And your index.html
file should look something like this:
Add the ClerkJS package using a <script>
tag
This <script>
tag will load the ClerkJS package 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 ClerkJS.
- Navigate to the Clerk Dashboard and select your application.
- In the navigation sidebar, select API Keys.
- In the Quick Copy section, select JavaScript from the dropdown menu.
- Copy the
<script>
tag and paste it into your HTML file.
It should look something like this:
In the <script>
tag, the onload
attribute is used to call the load()
method when the ClerkJS package is loaded. This is necessary to initialize ClerkJS. For more information on the load()
method and what options you can pass to it, check out the reference documentation.
Listen for the load
event
After the ClerkJS package is loaded, you can listen for the load
event to ensure that ClerkJS is ready to use.
Below the <script>
tag that initializes ClerkJS, add another <script>
tag to listen for the load
event:
Allow users to sign in or out
Clerk's prebuilt components are the easiest way to add authentication and user management to your application. They come styled out of the box and are customizable to fit your application's design.
To get started, you will use:
<SignIn />
: renders a user interface for signing in.<UserButton />
: shows the avatar from the account the user is signed in with. When clicked, it opens a dropdown menu with options to sign out.
Clerk Class Reference
Learn more about the Clerk class and how to use it.
Authentication Components
Learn more about all our authentication components.
Customization & Localization
Learn how to customize and localize the Clerk components.
Feedback
Last updated on