This guide covers how to handle errors from the Cred Protocol API, including common error codes, error response formats, and best practices for building resilient applications.
import { isAddress } from 'viem';function validateAddress(address) { // Check if it's a valid address or ENS name if (isAddress(address)) return true; if (address.endsWith('.eth')) return true; return false;}
async function handlePaymentRequired(response) { if (response.status === 402) { const data = await response.json(); // Option 1: Prompt user to authenticate console.log('Authentication options:', data.options); // Option 2: If using x402, process payment header const paymentRequired = response.headers.get('X-PAYMENT-REQUIRED'); // ... sign and retry with X-PAYMENT header }}
403 Forbidden
{ "detail": "Insufficient permissions" }
Causes:
Accessing a feature not in your plan
Account restrictions
Solutions:
Check your plan features in the Dashboard
Upgrade your plan if needed
Contact support for account issues
404 Not Found
{ "detail": "Address not found or has no on-chain activity" }
Causes:
Address has never been used on-chain
ENS name doesn’t exist
Incorrect endpoint path
Solutions:
Verify the address exists on Etherscan
Check ENS name resolution
Verify the endpoint URL
async function getScoreWithFallback(address) { try { const response = await fetch(`${API_URL}/api/v2/score/address/${address}`, { headers: { 'Authorization': `Bearer ${API_KEY}` } }); if (response.status === 404) { // Address has no activity - return a default or handle gracefully return { score: null, message: 'No on-chain activity found' }; } return response.json(); } catch (error) { throw error; }}
{ "detail": { "error": "credit_limit_exceeded", "message": "You have reached your monthly limit of 1,000 Cred Units. Your usage resets on day 15 of each month.", "plan": "free", "monthly_limit": 1000, "used": 1000, "remaining": 0, "period_end": "2024-02-15T00:00:00+00:00", "upgrade_url": "https://app.credprotocol.com/dashboard/billing" }}
Causes:
Exceeded monthly Cred Unit allocation
Response Fields:
Field
Description
error
Machine-readable error code
message
Human-readable explanation
plan
Current plan (free, pro, growth)
monthly_limit
Total CUs allowed per month
used
CUs consumed this billing period
remaining
CUs remaining (0 when limit reached)
period_end
When credits reset (ISO 8601)
upgrade_url
Link to upgrade plan
Solutions:
Wait until period_end for credits to reset
Upgrade your plan for more Cred Units
Optimize API usage by caching responses
Use summary endpoints (lower CU cost) where possible
async function handleCreditLimitError(response) { const error = await response.json(); if (error.detail?.error === 'credit_limit_exceeded') { const resetDate = new Date(error.detail.period_end); console.log(`Credit limit reached. Resets on ${resetDate.toLocaleDateString()}`); console.log(`Upgrade at: ${error.detail.upgrade_url}`); // Optionally notify user or disable API calls until reset return { limitReached: true, resetsAt: resetDate, upgradeUrl: error.detail.upgrade_url }; } throw new Error(error.detail);}
async function getScore(address) { const response = await fetch( `https://api.credprotocol.com/api/v2/score/address/${address}`, { headers: { 'Authorization': `Bearer ${API_KEY}` } } ); if (!response.ok) { const error = await response.json(); switch (response.status) { case 400: throw new Error(`Invalid request: ${error.detail}`); case 401: throw new Error('Authentication failed. Check your API key.'); case 404: throw new Error('Address not found or has no activity.'); case 429: throw new Error('Rate limit exceeded. Please try again later.'); default: throw new Error(`API error: ${error.detail}`); } } return response.json();}