Skip to content
Esc
navigateopen⌘Jpreview
Dashboard
On this page

Network interceptor

Intercept and modify outgoing backend SDK requests to the core in NodeJS, Python, and GoLang.

Overview

This hook intercepts all outgoing requests from the backend SDK to the core. Capture and modify the request before sending it to the core. Users can modify the HTTP method, query params, headers, and body of the request.

Prerequisites

Example

import { HttpRequest } from "supertokens-node/types";
import SuperTokens from "supertokens-node";

SuperTokens.init({
  supertokens: {
    connectionURI: "...",
    apiKey: "...",
    networkInterceptor: (request: HttpRequest, userContext: any) => {
      console.log("http request to core: ", request);
      // this can also be used to return a modified request object.
      return request;
    },
  },
  appInfo: {
    apiDomain: "...",
    appName: "...",
    websiteDomain: "...",
  },
  recipeList: [
    // ...
  ],
});
import (
    "log"
    "net/http"

    "github.com/supertokens/supertokens-golang/supertokens"
)

func main() {
supertokens.Init(supertokens.TypeInput{
        Supertokens: &supertokens.ConnectionInfo{
            ConnectionURI: "...",
            APIKey: "...",
            NetworkInterceptor: func(request *http.Request, context supertokens.UserContext) (*http.Request, error) {
				log.Print("http request to core: %+v", request)
				return request, nil
			},
        },
        AppInfo: supertokens.AppInfo{
            AppName: "...",
            APIDomain: "...",
            WebsiteDomain: "...",
        },
        RecipeList: []supertokens.Recipe{/*...*/},
    })
}
from typing import Dict, Any, Optional
from supertokens_python import init, InputAppInfo, SupertokensConfig

def intercept(
    url: str,
    method: str,
    headers: Dict[str, Any],
    params: Optional[Dict[str, Any]],
    body: Optional[Dict[str, Any]],
    user_context: Optional[Dict[str, Any]],
):
    print("http request to core: ", url, method, headers, params, body)
    return url, method, headers, params, body

init(
    app_info=InputAppInfo(
        app_name="...",
        api_domain="...",
        website_domain="...",
    ),
    supertokens_config=SupertokensConfig(
        connection_uri="...",
        api_key="...",
        network_interceptor=intercept,
    ),
    framework="django",  # works with other frameworks as well
    recipe_list=[
        # ...
    ],
)

API reference

API schema and response details