Using Your API Key
Once you receive your API key, you'll need to include it in all API requests:
// Example API request with key
const response = await fetch('https://api.crystara.trade/testnet/token-balances?address=0xABC&greaterThanZero=true',
{
headers:
{
'X-API-Key': 'your-api-key-here'
}
});
// Handle the response
const data = await response.json();
console.log(data);
API Key Security Best Practices
To keep your API key secure:
- Never expose your API key in client-side code - Always make API calls from your server
- Don't commit your API key to public repositories - Use environment variables instead
- Implement proper access controls - Restrict who can use your API key
- Monitor your usage - Watch for unexpected spikes that might indicate unauthorized use
- Rotate keys periodically - Request a new key if you suspect your current one is compromised
Example of proper API key storage using environment variables:
// .env file (not committed to repository)
CRYSTARA_API_KEY=your-api-key-here
Rate Limiting and Throttling
If you exceed your rate limit, the API will return a 429 (Too Many Requests) status code. To avoid this:
- Implement exponential backoff for retries
- Cache responses when appropriate
- Batch requests when possible
- Monitor your usage through our dashboard (coming soon)
Example implementation of exponential backoff:
async function fetchWithRetry(url, options, maxRetries = 5) {
let retries = 0;
while (retries < maxRetries) {
try {
const response = await fetch(url, {
...options,
headers: {
...options.headers,
'X-API-Key': process.env.CRYSTARA_API_KEY
}
});
if (response.status === 429) {
// Rate limited, wait and retry
const waitTime = Math.pow(2, retries) 1000;
console.log(Rate limited. Retrying in ${waitTime}ms);
await new Promise(resolve => setTimeout(resolve, waitTime));
retries++;
continue;
}
return await response.json();
} catch (error) {
console.error('API request failed:', error);
retries++;
if (retries >= maxRetries) {
throw new Error('Maximum retries reached');
}
const waitTime = Math.pow(2, retries) 1000;
await new Promise(resolve => setTimeout(resolve, waitTime));
}
}
}
Next Steps
After registering for an API key, explore our documentation to learn about:
- Token Balance Routes - Get token balances for a given address
- Ranking Routes - Get ranking data for collections and tokens
For any questions or support regarding API keys, contact our developer support team.