Skip to main content

Accessing protected APIs

Cloud has the newest features. Some may not be in a release yet; the newest release is Identity & Access Management 7.3.0.

Once a user is signed in, calls to your own APIs need the access token attached. Pick the client you use.

Call a protected API​

Using SDK Built-in HTTP Client​

When your application is wrapped with ThunderIDProvider, you can use the useThunderID hook to access the authenticated http module. This module has the following features: - Includes the necessary authentication headers (Bearer token) - Handles token refresh when tokens expire - Provides methods like request() and requestAll() for making API calls :::tip Accessing the HTTP Client You can access the http client in two ways: 1. Inside a component: Use the useThunderID hook to get the http instance 2. Outside a component (e.g., in utility functions or services): Import http directly :::

1. Basic API Request​

The following examples show how to use the Choreo Identity & Access Management SDK's http module to call a protected API endpoint. #### Using the Hook Inside a Component

src/UserProfile.jsx
import React, { useEffect, useState } from 'react'
import { useThunderID } from '@thunderid/react'

export default function UserProfile() {
const { http, isSignedIn } = useThunderID()
const [userData, setUserData] = useState(null)

useEffect(() => {
if (!isSignedIn) {
return
}

(async () => {
try {
const response = await http.request({
url: 'https://api.identity-platform.us.cloud.wso2.com/users/<user_id>',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
method: 'GET',
})

setUserData(response.data)
} catch (error) {
console.error('Error fetching user data:', error)
}
})()
}, [http, isSignedIn])

if (!isSignedIn) {
return <div>Please sign in to view your profile.</div>
}

return (
<div>
<h2>User Profile</h2>
{userData && <pre>{JSON.stringify(userData, null, 2)}</pre>}
</div>
)
}

2. Parallel API Requests​

To send multiple API requests in parallel, use the httpRequestAll function. It triggers parallel network requests and returns responses after all requests are completed. The following code snippet shows how to send multiple network requests in parallel:

src/UserProfile.jsx
import React, { useEffect, useState } from 'react'
import { useThunderID } from '@thunderid/react'

export default function UserProfile() {
const { http, isSignedIn } = useThunderID()
const [ userData, setUserData ] = useState({
profile: null,
applications: [],
})

useEffect(() => {
if (!isSignedIn) {
return
}

const requests = []

requests.push({
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
method: 'GET',
url: 'https://api.identity-platform.us.cloud.wso2.com/users/<user_id>',
})

requests.push({
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
method: 'GET',
url: 'https://api.identity-platform.us.cloud.wso2.com/applications',
})

(async () => {
try {
const response = await http.requestAll(requests)

setUserData({
profile: response[0].data,
applications: response[1].data,
})
} catch (error) {
console.error('Error fetching data:', error)
}
})()
}, [http, isSignedIn])

return <pre>{JSON.stringify(userData, null, 4)}</pre>
}

Using a GraphQL Client​

If you are not using webWorker as the storage type, use the getAccessToken function to fetch the access token. Then manually attach the token to the GraphQL client's authentication headers. :::note Storage Type Limitation This approach is not available when the storage type is set to webWorker. The SDK automatically manages the access token and does not expose it to the main thread. ::: The following example shows how to configure a GraphQL client with authentication using the access token: :::tip Replace `` with the actual user ID you want to fetch. This example uses Apollo Client, but you can apply the same pattern with other GraphQL clients like urql or graphql-request. :::

1. Set it up​

If you are not using webWorker as the storage type, use the getAccessToken function to fetch the access token. Then manually attach the token to the GraphQL client's authentication headers. :::note Storage Type Limitation This approach is not available when the storage type is set to webWorker. The SDK automatically manages the access token and does not expose it to the main thread. ::: The following example shows how to configure a GraphQL client with authentication using the access token:

src/graphql/client.js
import { ApolloClient, InMemoryCache, createHttpLink } from '@apollo/client'
import { setContext } from '@apollo/client/link/context'

export function createAuthenticatedClient(accessToken) {
const httpLink = createHttpLink({
uri: 'https://api.identity-platform.us.cloud.wso2.com/graphql',
})

const authLink = setContext((_, { headers }) => {
return {
headers: {
...headers,
authorization: accessToken ? `Bearer ${accessToken}` : '',
},
}
})

return new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache(),
})
}

Was this page helpful?