Skip to main content
Version: Legacy – v1.x

Calling Server APIs

Once you have confirmed that a user is logged in and has agreed to share their information, you can call the server API methods directly from JavaScript using the api() function. This function accepts an endpoint path as the first argument and a callback function as the second.

Profile API Call

For example, here's some code that calls the Profile API (i.e., the /me endpoint):

const ProfileBox = () => {
const { parallel, loginStatus } = useParallel()
const [profileResponse, setProfileResponse] = useState(null)

useEffect(() => {
// user isn't authenticated or library isn't loaded yet
if (loginStatus?.status === 'connected') parallel.api('/me', setProfileResponse)
}, [parallel, loginStatus])

if (!profileResponse) return null

if (profileResponse.type === 'individual') {
return <p>You are an individual named {profileResponse.profile.first_name}</p>
} else {
return <p>Your are a business named {profileResponse.profile.name}</p>
}
}

Accreditations API

Here's an example that calls the Accreditations API (i.e., the /accreditations endpoint):

const ProfileBox = () => {
const { parallel, loginStatus } = useParallel()
const [accreditationsResponse, setAccreditationsResponse] = useState(null)

useEffect(() => {
// user isn't authenticated or library isn't loaded yet
if (loginStatus?.status === 'connected') {
parallel.api('/accreditations', setAccreditationsResponse)
}
}, [parallel, loginStatus])

if (!accreditationsResponse) return null

if (accreditationsResponse.accreditations.some(({ status }) => status === 'current')) {
return <p>You are currently accredited!</p>
} else {
return <p>You are not currently accredited. But with Parallel you can get accredited!</p>
}
}