Fetch Token
This feature is currently in Early Availability (EA) status. For more information, see our product lifecycle phases.
Overview
This guide describes how to get a JWT token from the API so you can authenticate requests to your external API.
Step-by-step instructions
The fetchExternalData function simplifies token use by automatically adding the token to the request header.
- Begin by importing the
serviceAuthTokenobject.
import serviceAuthToken from "../../functions/serviceAuthToken";
- Call your external API using
serviceAuthToken.fetchExternalData.
The function returns a promise that resolves to an object containing the data from the external API. It automatically includes the JWT in the request header as MP-Authorization.
# Example curl command to demonstrate JWT token inclusion
curl -X GET "http://example.com/api/auth/v1/sessions/userinfo" \
-H "Accept: application/json" \
-H "MP-Authorization: Bearer YOUR_JWT_TOKEN_HERE"
Example of the fetchExternalData usage:
const { errorMessage, status, response } = await serviceAuthToken.fetchExternalData(
"http://example.com/api/auth/v1/sessions/userinfo", {
method: "GET",
credentials: "include",
headers: {
"Content-Type": "application/json",
}},
5000
);
Here, tokenInvalidTimer is set to 5000 ms (5 seconds).
Calls to the external endpoint specified by url are not cached.
Parameters
url: The endpoint URL.fetchOptions: An object in the same shape as thefetchAPI input, including all supported options.tokenInvalidTimer: (Optional) How long to cache the JWT in memory.
tokenInvalidTimer can be set from 0 ms to 180000 ms (3 minutes). The default is 180000 ms.
Return value
errorMessage: Any error message returned.status: The HTTP status (for example, 200, 500).response: The response from the endpoint.
Manually fetching the JWT token
To retrieve the token yourself for use in custom hooks, use these serviceAuthToken functions:
1. getServiceAuthToken
This function returns a promise that resolves to an object containing the JWT token.
Example with tokenInvalidTimer set to 5000 milliseconds:
const { errorMessage, status, token } = await serviceAuthToken.getServiceAuthToken(5000);
Parameters
tokenInvalidTimer: (Optional) How long to cache the token in memory.
tokenInvalidTimer can be from 0 ms to 180000 ms (3 minutes). The default is 180000 ms.
Return value
errorMessage: Any error message returned.status: The HTTP status (for example, 200, 500).token: The JWT token.
2. reset
Clears the cached JWT token.
Complete Example
import React, { useState } from "react";
import serviceAuthToken from "../../functions/serviceAuthToken";
interface UserInfo {
userName: string;
email: string;
}
const App = (): JSX.Element => {
const [userInfo, setUserInfo] = useState<UserInfo>();
const callAPI = async (): Promise<void> => {
const { errorMessage, status, response } =
await serviceAuthToken.fetchExternalData(
"http://example.com/api/auth/v1/sessions/userinfo", // specify your endpoint URL
{ // specify your fetchOptions
method: "GET",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
},
5000 // optional
);
const { userName, email } = response;
setUserInfo({ userName, email });
};
return (
<div data-e2e="microUIPage">
<h2>Hello World!</h2>
<div>Example API call to retrieve data:</div>
<div>Username: {userInfo?.userName}</div>
<div>Email: {userInfo?.email}</div>
<button type="button" onClick={callAPI}>
Call API
</button>
</div>
);
};
export default App;
Was this page helpful?
Tell us more…
Help us improve our content. Responses are anonymous.
Thanks
We appreciate your feedback!