Introduction
Postman is a popular API platform used by developers to design, build, test, and document APIs. It provides a user-friendly graphical interface (GUI) for interacting with APIs, making it easier to send requests, view responses, and debug issues.
In this article, we will walk through the steps required to configure Postman for use with the Crusoe Cloud API.
Step-by-Step Instructions
Step 1: Download OpenAPI Specification File
- Navigate to the Crusoe Cloud Docs.
- Go to Reference > API Reference.
- Click on the link for the full API reference.
- Click "Download" to download the OpenAPI specification file.
- Right click and "Save as..." to your documents, downloads, etc., leaving the filename the default openapi.json.
Alternatively, copy the URL of the OpenAPI specification file and save it for the next steps.
Step 2: Download/Install Postman
- Navigate to the Download Postman page on the official website.
- Download the client according to your OS.
- Install the client application.
Step 3: Import Collection
- Open the Postman application.
- Find "My Workspace" and click the "Import" button.
- Enter the URL of the OpenAPI specification file from earlier, or select the file which you downloaded.
- Select "Import as Postman Collection" and click "Import".
Step 4.1: Configure Collection – Scripts
- Navigate to the "Collections" tab in the Postman client.
- Select the "Crusoe Cloud API Gateway" folder/collection.
- Navigate to the "Scripts" > "Pre-request" tab.
- Add the following script contents.
const CryptoJS = require('crypto-js');
const URL = require('url');
const apiAccessKey = pm.variables.get("api_access_key");
const apiSecretKey = pm.variables.get("api_secret_key");
const signatureVersion = "1.0";
const baseUrl = pm.variables.get('baseUrl');
const requestPath = URL.parse(pm.request.url.toString().replace('{{baseUrl}}', baseUrl)).pathname
const requestVerb = pm.request.method;
const queryParams = pm.request.toJSON().url.query.filter((x)=>x.disabled!==true)
let canonicalizedQueryParams = "";
if (queryParams.length > 0) {
canonicalizedQueryParams = queryParams
.sort((a, b) => a.key.localeCompare(b.key))
.map(param => `${param.key}=${param.value}`)
.join('&') + "\n";
}
const dt = new Date().toISOString().replace(/\.\d{3}Z/, '+00:00')
pm.request.headers.add({
key: "X-Crusoe-Timestamp",
value: `${dt}`
});
const payload = `${requestPath}\n${canonicalizedQueryParams}\n${requestVerb}\n${dt}\n`;
try {
const decodedKey = CryptoJS.enc.Base64.parse(apiSecretKey.replace(/-/g, '+').replace(/_/g, '/') + '='.repeat((4 - apiSecretKey.length % 4) % 4));
const hmac = CryptoJS.HmacSHA256(payload, decodedKey);
const signature = CryptoJS.enc.Base64.stringify(hmac).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
pm.request.headers.add({
key: "Authorization",
value: `Bearer ${signatureVersion}:${apiAccessKey}:${signature}`
});
} catch (err) {
console.error("Error generating signature: ", err.message);
} - Click "Save" to save changes.
Step 4.2: Configure Collection – Variables
- Navigate to the "Variables" tab.
- Ensure the following variables are present.
Variable | Initial Value | Current Value |
baseUrl | https://api.crusoecloud.com/v1alpha5 | https://api.crusoecloud.com/v1alpha5 |
api_access_key | <ACCESS_KEY> | <ACCESS_KEY> |
api_secret_key | <SECRET_KEY> | <SECRET_KEY> |
Note: the API Access Key and API Secret Key are obtained separately, see: https://docs.crusoecloud.com/identity-and-security/managing-api-keys
Comments
0 comments
Article is closed for comments.