Work with multiple API endpoints - SuperTokens Docs

Work with multiple API endpoints

Configure sessions for multiple API endpoints using cookie and header-based authentication.

Overview

To enable use of sessions for multiple API endpoints, you need to update the configuration on both the frontend and backend.

Before you start

Steps

1. Set the cookie domain in the backend configuration

Set the cookieDomain value to be the common top level domain.
For example, if your API endpoints are {\"api.example.com\", \"api2.example.com\", \"api3.example.com\"}, the common portion of these endpoints is \".example.com\" (The dot is important). You would need to set the following:

Node.js

import SuperTokens from \"supertokens-node\";
import Session from \"supertokens-node/recipe/session\";

SuperTokens.init({
  supertokens: {
    connectionURI: \"...\",
  },
  appInfo: {
    apiDomain: \"...\",
    appName: \"...\",
    websiteDomain: \"...\",
  },
  recipeList: [\
    Session.init({\
      cookieDomain: \\".example.com\\",\
    }),\
  ],
});

Go

import (
    \"github.com/supertokens/supertokens-golang/recipe/session\"
    \"github.com/supertokens/supertokens-golang/recipe/session/sessmodels\"
    \"github.com/supertokens/supertokens-golang/supertokens\"
)

func main() {
    cookieDomain := \".example.com\"

supertokens.Init(supertokens.TypeInput{
        RecipeList: []supertokens.Recipe{
            session.Init(&sessmodels.TypeInput{
                CookieDomain: &cookieDomain,
            }),
        },
    })
}

Python

from supertokens_python import init, InputAppInfo
from supertokens_python.recipe import session

init(
    app_info=InputAppInfo(api_domain=\"...\", app_name=\"...\", website_domain=\"...\"),
    framework='...',
    recipe_list=[\
        session.init(\
            cookie_domain='.example.com'\
        )\
    ]
)

The above sets the session cookies’ domain to example.com, allowing them to send to *.example.com.

2. Set the older cookie domain in the backend configuration

To avoid locking out users with existing sessions (they get a 500 error when trying to refresh their session), set olderCookieDomain to match your previous cookieDomain.
If your cookieDomain was not set, you can use an empty string. However, if you don’t have any existing sessions, you can skip this step entirely.

Node.js

import SuperTokens from \"supertokens-node\";
import Session from \"supertokens-node/recipe/session\";

SuperTokens.init({
  supertokens: {
    connectionURI: \"...\",
  },
  appInfo: {
    apiDomain: \"...\",
    appName: \"...\",
    websiteDomain: \"...\",
  },
  recipeList: [\
    Session.init({\
      cookieDomain: \\".example.com\\",\
      olderCookieDomain: \"\", // Set to an empty string if your previous cookieDomain was unset. Otherwise, use your old cookieDomain value.\
    }),\
  ],
});

Go

func main() { cookieDomain := ".example.com" olderCookieDomain := "" // Set to an empty string if your previous cookieDomain was unset. Otherwise, use your old cookieDomain value.

supertokens.Init(supertokens.TypeInput{ RecipeList: []supertokens.Recipe{ session.Init(&sessmodels.TypeInput{ CookieDomain: &cookieDomain, OlderCookieDomain: &olderCookieDomain, }), }, }) }


#### Python

```python
from supertokens_python import init, InputAppInfo
from supertokens_python.recipe import session

init(
    app_info=InputAppInfo(api_domain=\"...\", app_name=\"...\", website_domain=\"...\"),
    framework='...',
    recipe_list=[\
        session.init(\
            cookie_domain='.example.com',\
            older_cookie_domain='' # Set to an empty string if your previous cookie_domain was unset. Otherwise, use your old cookie_domain value.\
        )\
    ]
)

3. Update the frontend configuration

Set the same value for sessionTokenBackendDomain on the frontend.
This allows the frontend SDK to apply interception and automatic refreshing across all your API calls:

UI type

Pre-built UIDrop-in components that handle the flows for you.Custom UIBuild your own UI and call the SDK directly.

Angular

You need to make changes to the auth route configuration, as well as to the supertokens-web-js SDK configuration at the root of your application:

React

import SuperTokens from \"supertokens-auth-react\";
import Session from \"supertokens-auth-react/recipe/session\";

SuperTokens.init({
  appInfo: {
    apiDomain: \"...\",
    appName: \"...\",
    websiteDomain: \"...\",
  },
  recipeList: [\
    Session.init({\
      sessionTokenBackendDomain: \\".example.com\\",\
    }),\
  ],
});

Angular

This change goes in the supertokens-web-js SDK configuration at the root of your application:

import SuperTokens from \"supertokens-web-js\";
import Session from \"supertokens-web-js/recipe/session\";

SuperTokens.init({
  appInfo: {
    apiDomain: \"...\",
    appName: \"...\",
  },
  recipeList: [\
    Session.init({\
      sessionTokenBackendDomain: \\".example.com\\",\
    }),\
  ],
});

Platforms

import SuperTokens from \"supertokens-web-js\";
import Session from \"supertokens-web-js/recipe/session\";

- **Script tag**

```javascript
supertokens.init({
  appInfo: {
    apiDomain: \"...\",
    appName: \"...\",
  },
  recipeList: [\
    supertokensSession.init({\
      sessionTokenBackendDomain: \\".example.com\\",\
    }),\
  ],
});

ReactNative

import SuperTokens from \"supertokens-react-native\";

SuperTokens.init({
  apiDomain: \"...\",
  sessionTokenBackendDomain: \".example.com\",
});

Android

import android.app.Application
import com.supertokens.session.SuperTokens

class MainApplication: Application() {
    override fun onCreate() {
        super.onCreate()

SuperTokens.Builder(this, \"...\")
            .sessionTokenBackendDomain(\".example.com\")
            .build()
    }
}

iOS

import UIKit
import SuperTokensIOS

fileprivate class ApplicationDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        do {
            try SuperTokens.initialize(
                apiDomain: \"...\",
                sessionTokenBackendDomain: \".example.com\"
            )
        } catch SuperTokensError.initError(let message) {
            // TODO: Handle initialization error
        } catch {
            // Some other error
        }

return true
    }
}

Flutter

import 'package:supertokens_flutter/supertokens.dart';

void initialiseSuperTokens() {
    SuperTokens.init(
        apiDomain: \"...\",
        sessionTokenBackendDomain: \".example.com\",
    );
}