Last updated: 2026-04-06
Reference
Node.js Code Samples¶
Production-ready Node.js examples for integrating with Dxtra's privacy compliance API.
Prerequisites¶
Requirements
- Node.js 18+ installed on your machine (22+ recommended)
- A Dxtra API key - see Authentication Guide
- Basic knowledge of JavaScript/Node.js and async programming
Making a GraphQL Query¶
This example shows how to make a simple query to the Dxtra GraphQL API using the axios library.
First, install axios:
Then, use the following code to fetch a list of data controllers:
JavaScript
const axios = require('axios');
const DXTRA_API_KEY = 'YOUR_API_KEY';
const DXTRA_AUTH_URL = 'https://auth.dxtra.ai/v1/signin/pat';
const DXTRA_API_URL = 'https://api.dxtra.ai/v1/graphql';
async function exchangeApiKeyForJWT(apiKey) {
try {
const response = await axios.post(DXTRA_AUTH_URL, {
personalAccessToken: apiKey
});
return response.data.session.accessToken;
} catch (error) {
console.error('Error exchanging API key for JWT:', error);
throw error;
}
}
async function getDataControllers() {
try {
// Step 1: Exchange API key for JWT
const jwtToken = await exchangeApiKeyForJWT(DXTRA_API_KEY);
// Step 2: Use JWT to make GraphQL request
const response = await axios.post(
DXTRA_API_URL,
{
query: `
query GetDataControllers {
dataControllers {
id
title
did
createdAt
}
}
`,
},
{
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${jwtToken}`,
'X-Hasura-Role': 'user',
},
}
);
console.log(response.data.data.dataControllers);
} catch (error) {
console.error('Error fetching data controllers:', error);
}
}
getDataControllers();
API Key Security
Remember to replace YOUR_API_KEY with your actual Dxtra API key. Never commit API keys to version control - use environment variables instead.
This example demonstrates the proper two-step authentication flow:
- Exchange API key for JWT: Use the PAT (Personal Access Token) endpoint
- Make GraphQL requests: Use the JWT token with proper role headers
Advanced Examples¶
Environment Variables Setup¶
Secure Configuration Pattern
require('dotenv').config();
const config = {
apiKey: process.env.DXTRA_API_KEY,
authUrl: process.env.DXTRA_AUTH_URL || 'https://auth.dxtra.ai/v1/signin/pat',
apiUrl: process.env.DXTRA_API_URL || 'https://api.dxtra.ai/v1/graphql',
};
// Validate required environment variables
if (!config.apiKey) {
throw new Error('DXTRA_API_KEY environment variable is required');
}
Error Handling with Retry Logic¶
Production Error Handling
const axios = require('axios');
class DxtraApiClient {
constructor(apiKey, options = {}) {
this.apiKey = apiKey;
this.baseUrl = options.baseUrl || 'https://api.dxtra.ai/v1/graphql';
this.authUrl = options.authUrl || 'https://auth.dxtra.ai/v1/signin/pat';
this.jwtToken = null;
}
async authenticate() {
try {
const response = await axios.post(this.authUrl, {
personalAccessToken: this.apiKey
});
this.jwtToken = response.data.session.accessToken;
return this.jwtToken;
} catch (error) {
throw new Error(`Authentication failed: ${error.response?.data?.message || error.message}`);
}
}
async query(graphqlQuery, variables = {}, retries = 3) {
if (!this.jwtToken) {
await this.authenticate();
}
for (let attempt = 1; attempt <= retries; attempt++) {
try {
const response = await axios.post(
this.baseUrl,
{
query: graphqlQuery,
variables
},
{
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.jwtToken}`,
'X-Hasura-Role': 'user',
},
}
);
// Handle GraphQL errors
if (response.data.errors) {
throw new Error(`GraphQL Error: ${response.data.errors[0].message}`);
}
return response.data.data;
} catch (error) {
// Handle 401 by re-authenticating
if (error.response?.status === 401 && attempt < retries) {
console.log('Token expired, re-authenticating...');
await this.authenticate();
continue;
}
// Handle rate limits with exponential backoff
if (error.response?.status === 429 && attempt < retries) {
const delay = Math.pow(2, attempt) * 1000;
console.log(`Rate limited, retrying in ${delay}ms...`);
await new Promise(resolve => setTimeout(resolve, delay));
continue;
}
throw error;
}
}
}
}
Using the Client¶
Client Usage Example
const client = new DxtraApiClient(process.env.DXTRA_API_KEY);
async function fetchDataControllers() {
try {
const query = `
query GetDataControllers {
dataControllers {
id
title
did
createdAt
}
}
`;
const data = await client.query(query);
console.log('Data Controllers:', data.dataControllers);
return data.dataControllers;
} catch (error) {
console.error('Failed to fetch data controllers:', error.message);
process.exit(1);
}
}
fetchDataControllers();
Related Documentation¶
- Authentication Guide - Detailed authentication setup
- GraphQL Reference - Complete API schema
- Error Handling - Comprehensive error patterns