---
description: Integrate Authgear to your website with the Web SDK
---
# JavaScript (Web)
[](https://raw.githubusercontent.com/authgear/docs/refs/heads/main/get-started/single-page-app/website.md)
In this guide, you'll learn how to integrate Authgear into your website using the Token Approach. In the token approach, the Authgear server returns an access token and a refresh token to your SPA application after successful user authentication.
Your application can send the access token in subsequent HTTP requests to access protected resources.
Follow this guide to add Authgear SDK to any web application in under 🕐 **10 minutes**.
{% hint style="info" %}
See and clone the full code for the demo app for this tutorial in [**the Github Repo here**](https://github.com/authgear/authgear-example-spa-js).
{% endhint %}
This guide uses the Authgear Web SDK for integrating Authgear with a SPA Web app. Supported browsers include:
* Last 2 Firefox major versions
* Last 2 Chrome major versions
* Last 2 Edge major versions
* Last 3 Safari major versions
## Setup Application in Authgear
Signup for an Authgear Portal account in [https://portal.authgear.com/](https://portal.authgear.com/). Or you can use your self-deployed Authgear.
From the Project listing, create a new Project or select an existing Project. After that, we will need to create an application in the project.
### **Step 1: Create an application in the Portal**
Go to **Applications** on the left menu bar.
Click **⊕Add Application** in the top toolbar.
Input the name of your application and select the application type **Single Page Application**. Click the **Save** button to proceed.
create spa application authgear portal
On the next screen you will see a list of tutorials for different frameworks, click on **Next** to skip to your client application configuration page.
### **Step 2: Configure the application**
First, decide the paths in your website that users will be redirected to after they have authenticated with Authgear.
To run the demo app in this tutorial offline, scroll to the URIs section of your client application page add the following URI:
Under **Authorized Redirect URIs** add `http://localhost:3000/`
{% hint style="info" %}
Note that the trailing "/" in the above URLs must be included.
{% endhint %}

Click on the **Save** button to keep your changes.
## Add Authgear to any web page using JavaScript
In this section, we'll create a simple web page and use the Authgear Web JavaScript SDK to add user authentication to the page.
### Step 1: Create a Basic Single Page Web Application
For this guide, we'll create a basic web application with only one page. Follow these steps to set up the application. First, create a new directory on your computer for the project. You can do this in a terminal using the following commands:
```bash
mkdir my-webapp
cd my-webapp
```
### Step 2: Create Web Server
We'll create an Express.js server in the web project directory so that we can access the HTML page in a web browser using `http://localhost:3000`. Run the following commands from the root of your web project directory to install Express.
First, generate a package.json file in your project directory:
```bash
npm init -y
```
Install the Express npm package:
```bash
npm install express
```
Install Nodemon (used for adding hot road to JavaScript development):
```sh
npm install -D nodemon
```
Now create a `server.js` file in the root folder of your project. Add the following code to server.js:
```javascript
const express = require("express");
const { join } = require("path");
const app = express();
// Serve static assets from the /public folder
app.use(express.static(join(__dirname, "public")));
// Serve the index page for all other requests
app.get("/*", (_, res) => {
res.sendFile(join(__dirname, "index.html"));
});
// Listen on port 3000
app.listen(3000, () => console.log("Application running on port 3000"));
```
Next, open the `package.json` in your project directory and add the following lines in the script section:
```json
"start": "node server.js",
"dev": "nodemon server.js"
```
{% hint style="info" %}
Note that the above you may use your preferred tool/environment to serve the HTML file and skip the step of creating an Express server.
{% endhint %}
### Step 3: Create Web Page
Create a new file called `index.html` in the root of your project directory. Add the following code to the file:
```html
Authgear SPA SDK Sample
SPA Authentication Sample
Welcome to our page!
```
The content `index.html` is a simple web page that contains some text, a Log in button, and a Log out button. In later steps, we will implement the `onclick` events for both steps such that each calls the correct function from the Authgear Web JavaScript SDK.
### Step 4: Install Authgear Web JavaScript SDK
#### CDN
The Web JS SDK is available on a CDN that you can include in any webpage using the following script tag:
```html
```
The Web JS SDK is also available as [an npm package](https://www.npmjs.com/package/@authgear/web). That can be installed using any of the following commands:
#### NPM
```bash
npm install --save --save-exact @authgear/web
```
#### Yarn
```bash
yarn add @authgear/web --exact
```
We recommend that you use the npm package to add Authgear to your web application when you're using build tools like Vite and Webpack, and when building with frameworks like React, Vue, Angular, etc.
The easiest way to add a JavaScript library such as the Authgear SDK to a generic Single Page Application (a basic `.html` file page), is to use a CDN. Hence, we'll use the CDN method to add the Authgear Web JavaScript SDK to our demo application for this tutorial.
To install the Authgear SDK, add the Authgear SDK CDN \