Skip to main content
Version: 8.1.X

Sending Requests with Axios

Step 1) Add axios interceptors#

import SuperTokens from 'supertokens-website';

SuperTokens.addAxiosInterceptors(axios);
important

To be called on each axios instances that is being imported.

Step 2) Use axios as usual#

import SuperTokens from 'supertokens-website';
import axios from "axios";
SuperTokens.addAxiosInterceptors(axios);

SuperTokens.init({
apiDomain: "https://api.example.com"
});

async function doAPICalls() {
try {
let postData = { ... };
let response = await axios({url: "someAPI", method: "post", data: postData });
let data = await response.data;
let someField = data.someField;
} catch (err) {
if (err.response !== undefined && err.response.status === 401) {
// redirect user to login
} else {
// handle error
}
}
}
Which frontend SDK do you use?
supertokens-web-js / mobile
supertokens-auth-react