Skip to main content

If you are using our backend SDK that is lesser than the following versions, please visit the older documentation link here.

Adding and reading custom request properties

When using the APIs exposed by the SuperTokens SDKs, you may want to pass custom information to your backend. You can leverage our pre api hook, overrides and user context features to achieve this by:

  1. Using the pre api hook to add custom information to network requests made by our frontend SDKs
  2. Using the overrides feature to provide custom handling for APIs and functions in the backend SDK
  3. Using the user context feature in the backend to access to original request and consuming any custom information

Adding custom information to requests on the frontend#

You can use our pre api hook feature to add custom information to network requests made by the frontend SDKs.

For example let us consider a React app using supertokens-auth-react where we add some custom header whenever the user signs out:

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

Session.init({
preAPIHook: async (context) => {
let requestInit = context.requestInit;

if (context.action === "SIGN_OUT") {
let headers = {
...requestInit.headers,
customHeader: "customvalue,"
};
requestInit = {
...requestInit,
headers,
}
}

return {
url: context.url,
requestInit,
};
}
})
info

Refer to the pre api hook documentation to learn how to do this for our other frontend SDKs

Reading custom request information in the backend#

To read information on the backend we need to use either the API overrides feature or the backend function override feature. We override the API/function we want to read the information in, get the original request object and then read the query/body to consume the custom property.

Let us continue the example we used above, we need to read the headers from the request and read the value of customHeader. This will involve:

  • Overriding either the revokeSession function or the signOutPOST API of the session recipe
  • Getting the request object from the user context
  • Reading the custom header value

We use the getRequestFromUserContext function provided by the SDK to get the request object from the user context.

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

Session.init({
override: {
functions: (oI) => {
return {
...oI,
revokeSession: async (input) => {
let customHeaderValue = "";
const request = SuperTokens.getRequestFromUserContext(input.userContext);

if (request !== undefined) {
customHeaderValue = request.getHeaderValue("customHeader");
} else {
/**
* This is possible if the function is triggered from the user management dashboard
*
* In this case set a reasonable default value to use
*/
customHeaderValue = "default";
}


// Perform custom logic based on the value of customHeaderValue

return oI.revokeSession(input);
},
};
},
apis: (oI) => {
return {
...oI,
signOutPOST: async (input) => {
if (oI.signOutPOST === undefined) {
throw Error("Signout API is disabled");
}

let customHeaderValue = "";
const request = SuperTokens.getRequestFromUserContext(input.userContext);

if (request !== undefined) {
customHeaderValue = request.getHeaderValue("customHeader");
} else {
/**
* This is possible if the function is triggered from the user management dashboard
*
* In this case set a reasonable default value to use
*/
customHeaderValue = "default";
}


// Perform custom logic based on the value of customHeaderValue

return oI.signOutPOST(input);
},
};
}
},
})
Looking for older versions of the documentation?
Which UI do you use?
Custom UI
Pre built UI