Quickstart
Make your first API call!
Introduction
The Unifize APIs are crafted following RESTful standards, ensuring that interaction with resources is consistent and intuitive. The APIs are endpoint-driven and typically represent distinct resources, allowing for clear, organized, and efficient communication between systems.
All interactions are managed through the JSON data format, which necessitates setting the content type to application/json
. This requirement is enforced for both request and response bodies unless otherwise specified, facilitating seamless data exchange and integration with various applications. Such consistent data handling simplifies interaction and enhances compatibility with different platforms and programming environments.
Authentication and Authorization
Authentication is a necessary aspect of utilizing the Unifize APIs and is implemented via app tokens. These tokens are generated according to the guidelines detailed in the App Tokens page.
Additionally, authorization is handled via a permissions framework, which grants granular control over access levels and operations. Permissions can be set initially during app creation and remain flexible, allowing for modifications as the needs of the application evolve. This dual-layered security model ensures that the Unifize APIs provide robust protection for sensitive operations and data.
Organizations
Central to Unifize's architecture are logical entities known as "Organizations," which help partition and manage resources within the system. The Org Id of these organizations must be specified during the initial token request process. This token, once embedded within a JWT, eliminates the need for repeatedly specifying the Org Id in further API calls, streamlining authentication processes and reducing complexity.
You can read more about Organizations in the Concepts and Terminologies page.
Test it out
To do this using your preferred programming language, the only requirements are:
A library to mint JWTs
A library to read/parse PEM files
A library to make network requests
Below are some examples in commonly used programming languages.
Also see the API Reference.
Prerequisites:
npm install jsonwebtoken axios
const fs = require('fs');
const jwt = require('jsonwebtoken');
const axios = require('axios');
/**
* Loads a private key from a PEM file
* @param {string} filePath - Path to the PEM file
* @returns {string} Private key as a string
*/
function loadPrivateKeyFromPem(filePath) {
return fs.readFileSync(filePath, 'utf8');
}
/**
* Generates a JWT token with the given app ID and signed using the private key
* @param {number} appId - The application ID to use as issuer
* @param {string} privateKeyPath - Path to the private key PEM file
* @returns {string} Signed JWT token
*/
function generateJwt(appId, privateKeyPath) {
// Load the private key
const privateKey = loadPrivateKeyFromPem(privateKeyPath);
// Get current time in seconds
const nowSeconds = Math.floor(Date.now() / 1000);
// Create the payload
const payload = {
iss: appId.toString(),
iat: nowSeconds,
exp: nowSeconds + 600
};
// Sign the JWT
return jwt.sign(payload, privateKey, { algorithm: 'RS256' });
}
/**
* Exchanges the JWT for an access token
* @param {string} jwtToken - The JWT token to exchange
* @param {number} orgId - The organization ID
* @returns {Promise} Promise resolving to the access token response
*/
async function exchangeJwtForAccessToken(jwtToken, orgId) {
try {
const response = await axios.post(
`https://api.example.com/application/installation/${orgId}/token`,
{ token: jwtToken },
{
headers: {
'Authorization': `Bearer ${jwtToken}`,
'Content-Type': 'application/json'
}
}
);
return response.data;
} catch (error) {
throw error;
}
}
/**
* Makes an API call with the access token in the Authorization header
* @param {string} accessToken - The access token to use for authorization
* @returns {Promise} Promise resolving to the API response
*/
async function makeApiCall(accessToken) {
try {
const response = await axios.get('https://api.example.com/', {
headers: {
'Authorization': `Bearer ${accessToken}`
}
});
return response;
} catch (error) {
throw error;
}
}
/**
* Main function to demonstrate the JWT generation, token exchange, and API call
*/
async function main() {
// Example usage
const appId = 12345;
const orgId = 67890;
const privateKeyPath = 'path/to/private_key.pem';
try {
// Generate JWT
const jwt = generateJwt(appId, privateKeyPath);
console.log('JWT generated successfully');
// Exchange JWT for access token
const tokenResponse = await exchangeJwtForAccessToken(jwt, orgId);
const accessToken = tokenResponse.access_token;
const expiresAt = tokenResponse.expires_at;
console.log(`Access token received, expires at: ${expiresAt}`);
// Make API call with access token
const response = await makeApiCall(accessToken);
// Print response
console.log(`Status code: ${response.status}`);
console.log(`Response body:`, response.data);
} catch (error) {
console.error('Error:', error.message);
if (error.response) {
console.error('Response status:', error.response.status);
console.error('Response data:', error.response.data);
}
}
}
// Execute the main function
main();
Last updated