JavaScript Quickstart
Before you start
Example repository
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.
Create a new JavaScript app
To install Clerk's JavaScript SDK, you need to use a bundler like Vite or Webpack.
If you don't already have a JavaScript app, run the following commands to create a new one using Vite. From the prompts, choose the Vanilla framework, and then choose the JavaScript variant.
npm create vite@latest clerk-javascript
cd clerk-javascript
npm installpnpm create vite clerk-javascript
cd clerk-javascript
pnpm installyarn create vite clerk-javascript
cd clerk-javascript
yarn installbunx create-vite clerk-javascript
cd clerk-javascript
bun installInstall @clerk/clerk-js
The Clerk JavaScript SDK gives you access to a set of classes and methods to make user authentication easier.
Run the following command to install the SDK:
npm install @clerk/clerk-jspnpm add @clerk/clerk-jsyarn add @clerk/clerk-jsbun add @clerk/clerk-jsSet your Clerk API keys
It's recommended to use environment variables to store your Clerk . 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.
Add your Clerk to your .env file.
- In the Clerk Dashboard, navigate to the API keys page.
- In the Quick Copy section, copy your Clerk .
- Paste your key into your
.envfile.
The final result should resemble the following:
VITE_CLERK_PUBLISHABLE_KEY=YOUR_PUBLISHABLE_KEYInitialize Clerk
To initialize Clerk, add the following code to your main.js file. This code imports your , creates a new Clerk instance, and calls the load() method to initialize Clerk.
import { Clerk } from '@clerk/clerk-js'
const publishableKey = import.meta.env.VITE_CLERK_PUBLISHABLE_KEY
const clerk = new Clerk(publishableKey)
await clerk.load({
// Set load options here
})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:
import { Clerk } from '@clerk/clerk-js'
const publishableKey = import.meta.env.VITE_CLERK_PUBLISHABLE_KEY
const clerk = new Clerk(publishableKey)
await clerk.load()
if (clerk.isSignedIn) {
document.getElementById('app').innerHTML = `
<div id="user-button"></div>
`
const userButtonDiv = document.getElementById('user-button')
clerk.mountUserButton(userButtonDiv)
} else {
document.getElementById('app').innerHTML = `
<div id="sign-in"></div>
`
const signInDiv = document.getElementById('sign-in')
clerk.mountSignIn(signInDiv)
}And your index.html file should look something like this:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite App</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>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:5173.
- Select "Sign up" on the page and authenticate 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 and . 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:
<!-- Rest of your HTML file -->
<script
async
crossorigin="anonymous"
data-clerk-publishable-key="YOUR_PUBLISHABLE_KEY"
src="https://YOUR_FRONTEND_API_URL/npm/@clerk/clerk-js@5/dist/clerk.browser.js"
type="text/javascript"
></script>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:
<!-- Rest of your HTML file -->
<script>
window.addEventListener('load', async function () {
await Clerk.load()
console.log('ClerkJS is loaded')
})
</script>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.
<div id="app"></div>
<!-- Initialize Clerk with your
Clerk Publishable Key and Frontend API URL -->
<script
async
crossorigin="anonymous"
data-clerk-publishable-key="YOUR_PUBLISHABLE_KEY"
src="https://YOUR_FRONTEND_API_URL/npm/@clerk/clerk-js@5/dist/clerk.browser.js"
type="text/javascript"
></script>
<script>
window.addEventListener('load', async function () {
await Clerk.load()
if (Clerk.isSignedIn) {
document.getElementById('app').innerHTML = `
<div id="user-button"></div>
`
const userButtonDiv = document.getElementById('user-button')
Clerk.mountUserButton(userButtonDiv)
} else {
document.getElementById('app').innerHTML = `
<div id="sign-in"></div>
`
const signInDiv = document.getElementById('sign-in')
Clerk.mountSignIn(signInDiv)
}
})
</script>Next steps
Learn more about Clerk components, how to customize them, and how to use Clerk's JavaScript classes using the following guides.
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