Skip to main content
Version: 2.0

Code Tutorial

This tutorial provides a working knowledge of the API through a series of minimal code examples.

Step 1: Get an ID token

An ID token can be used to manage organizations within PDK. The following example uses the client credentials authentication flow to get an ID token with a single request.

const clientId = "{{client_id}}"
const clientSecret = "{{client_secret}}"
const systemId = "{{system_id}}"

async function getIdToken() {
const response = await fetch("https://accounts.pdk.io/oauth2/token", {
method: "POST",
headers: {
Authorization: "Basic " + btoa(`${clientId}:${clientSecret}`),
"Content-Type": "application/x-www-form-urlencoded"
},
body: "grant_type=client_credentials"
})
const data = await response.json()
return data.id_token
}

Step 2: Get a system token

An ID token can also be used to get a system token. The following example returns a system token for a given system ID, which can be obtained from the pdk.io URL.

async function getSystemToken(systemId, idToken) {
const response = await fetch(`https://accounts.pdk.io/api/systems/${systemId}/token`, {
method: "POST",
headers: { Authorization: `Bearer ${idToken}` }
})
const data = await response.json()
return data.token
}
tip

Rather than retrieving fresh ID and system tokens before every request, applications can increase speed and efficiency by re-using tokens whenever possible. For example, an application interacting with a large number of systems should only need to retrieve a new ID token every 5 minutes. That single ID token can then be used to retrieve system tokens for every system, and each system token can be used many times during its 5-minute lifetime.

Step 3: Use the system token

A system token can be used to manage the properties of a particular system, including holders, credentials, groups, and rules. The following example creates a new holder on a given system.

async function createHolder(systemId, systemToken) {
const response = await fetch(`https://systems.pdk.io/${systemId}/holders`, {
method: "POST",
headers: {
Authorization: `Bearer ${systemToken}`,
"Content-Type": "application/json"
},
body: '{"firstName":"John","lastName":"Wiegand"}'
})
}

Step 4: Put it all together

These examples can be combined to create a functional script with no dependencies.

async function run() {
const idToken = await getIdToken()
const systemToken = await getSystemToken(systemId, idToken)
createHolder(systemId, systemToken)
}

run()