Skip to main content

Backend Integration

1) Install#

npm i -s supertokens-node

2) Initialise SuperTokens#

Please fill the form below to see the code snippet (* = Required)
Your app's name:*
Information about the question
This is the name of your application
API Domain:*
Information about the question
This is the URL of your app's API server.
API Base Path:
Information about the question
SuperTokens will expose it's APIs scoped by this base API path.
Website Domain:*
Information about the question
This is the URL of your website.
Website Base Path:
Information about the question
SuperTokens UI will be shown on this website route.
Submit form

3) Add the SuperTokens APIs & CORS setup#

Please fill the form below to see the code snippet (* = Required)
API Base Path:
Information about the question
SuperTokens will expose it's APIs scoped by this base API path.
Website Domain:*
Information about the question
This is the URL of your website.
Submit form

4) Add the SuperTokens error handler#

import express, { Request, Response, NextFunction } from "express";import { errorHandler } from "supertokens-node/framework/express";
let app = express();
// ...your API routes
// Add this AFTER all your routesapp.use(errorHandler())
// your own error handlerapp.use((err: unknown, req: Request, res: Response, next: NextFunction) => { /* ... */ });

5) Add session verification to your API#

For your APIs that require a user to be logged in, use the verifySession middleware:

import express from "express";import { verifySession } from "supertokens-node/recipe/session/framework/express";import { SessionRequest } from "supertokens-node/framework/express";
let app = express();
app.post("/like-comment", verifySession(), (req: SessionRequest, res) => {    let userId = req.session!.getUserId();    //....});

6) Setup the SuperTokens core#

Are you using https://try.supertokens.com as the connection URI in the init function?
YesNo