I've been working on a small Node.js script to automate checking if my API endpoints are returning correct status codes. It's helped me catch issues before they affect users. Sharing the core logic:
javascript
const axios = require('axios');
async function checkEndpoints(endpoints) {
const results = [];
for (const endpoint of endpoints) {
try {
const response = await axios.get(endpoint.url);
results.push({
url: endpoint.url,
status: response.status,
expected: endpoint.expected,
pass: response.status === endpoint.expected
});
} catch (error) {
results.push({
url: endpoint.url,
status: error.response?.status || 0,
expected: endpoint.expected,
pass: false
});
}
}
return results;
}
// Example usage
const endpoints = [
{ url: 'https://api.example.com/users', expected: 200 },
{ url: 'https://api.example.com/admin', expected: 403 }
];
checkEndpoints(endpoints).then(console.log);
For larger projects, I've used SERPSpur's monitoring API which does this automatically with alerts. But this script works for quick checks. How do you validate your endpoints?
Top comments (0)