When requests fail, systematic debugging helps identify the issue quickly.
Enable detailed logging
async function debugRequest(url, options) {
console.log('=== API Request ===");
console.log('URL:', url);
console.log('Method:', options.method);
console.log('Headers:', {
...options.headers,
'x-api-key': options.headers['x-api-key']?.substring(0, 10) + '...'
});
console.log('Body:', options.body);
const startTime = Date.now();
try {
const response = await fetch(url, options);
const duration = Date.now() - startTime;
console.log('=== API Response ===");
console.log('Status:', response.status, response.statusText);
console.log('Duration:', duration + 'ms');
console.log('Headers:', Object.fromEntries(response.headers));
const data = await response.json();
console.log('Body:', data);
return { response, data };
} catch (error) {
console.error('=== Request Failed ===");
console.error('Error:', error.message);
console.error('Duration:', Date.now() - startTime + 'ms');
throw error;
}
}
Check these first
1. API key is correct:
console.log('API key starts with:', apiKey.substring(0, 10));
console.log('API key length:', apiKey.length);
2. Header name is exact:
// Must be lowercase 'x-api-key'
headers: {
'x-api-key': apiKey, // ✓ Correct
'X-API-Key': apiKey, // ✗ Wrong
'api-key': apiKey, // ✗ Wrong
}
3. JSON is valid:
try {
const body = JSON.stringify({ query: 'test' });
console.log('Valid JSON:', body);
} catch (e) {
console.error('Invalid JSON:', e);
}
4. Request reaches the server:
// Use browser DevTools Network tab or
curl -v -X POST https://api.cathopic.com/resources \
-H "x-api-key: YOUR_KEY" \
-H "Content-Type: application/json" \
-d '{"query": "test"}'
Common mistakes
Wrong Content-Type:
// ✗ Wrong
headers: {
'Content-Type': 'text/plain'
}
// ✓ Correct
headers: {
'Content-Type': 'application/json'
}
Not stringifying body:
// ✗ Wrong
body: { query: 'test' }
// ✓ Correct
body: JSON.stringify({ query: 'test' })
Using GET instead of POST:
// ✗ All endpoints require POST
method: 'GET'
// ✓ Correct
method: 'POST'
Test with minimal request
Strip down to bare minimum to isolate the problem:
const response = await fetch('https://api.cathopic.com/resources', {
method: 'POST',
headers: {
'x-api-key': 'YOUR_ACTUAL_KEY_HERE',
'Content-Type': 'application/json'
},
body: '{}' // Empty object
});
console.log(response.status);
console.log(await response.json());
If this works, gradually add back parameters until it fails.
Compare with working example
Test with curl to verify your implementation:
curl -X POST https://api.cathopic.com/resources \
-H "x-api-key: YOUR_KEY" \
-H "Content-Type: application/json" \
-d '{"query": "Jesus", "page": 1, "page_size": 5}'
If curl works but your code doesn't, compare the exact headers and body.
Still stuck?
Contact developer support with:
Full error message
Request headers (hide your full API key)
Request body
Response status and body
Programming language and HTTP library
Code snippet (without exposing credentials)