Testing & Debugging
Learn how to effectively test your Zyhap SMM API integration and debug common issues.
Testing Strategy
Unit Tests
Test individual API calls and response handling in isolation.
Integration Tests
Test complete workflows from API calls to data processing.
End-to-End Tests
Test real scenarios with actual social media posting.
Unit Testing
Test your API client methods with mocked responses:
// __tests__/zyhap-client.test.js
const ZyhapClient = require('../src/zyhap-client');
// Mock axios
jest.mock('axios');
const axios = require('axios');
describe('ZyhapClient', () => {
let client;
beforeEach(() => {
client = new ZyhapClient('test-api-key');
axios.mockClear();
});
test('creates post successfully', async () => {
const mockResponse = {
data: {
id: 'post_123',
content: 'Test post',
status: 'scheduled'
}
};
axios.post.mockResolvedValue(mockResponse);
const result = await client.createPost({
content: 'Test post',
platforms: ['instagram']
});
expect(axios.post).toHaveBeenCalledWith(
'https://api.zyhap.com/api/v1/public/marketing/posts',
expect.objectContaining({
content: 'Test post',
platforms: ['instagram']
}),
expect.objectContaining({
headers: {
'Authorization': 'Bearer test-api-key',
'Content-Type': 'application/json'
}
})
);
expect(result.id).toBe('post_123');
});
test('handles API errors gracefully', async () => {
const mockError = {
response: {
status: 400,
data: { message: 'Invalid content' }
}
};
axios.post.mockRejectedValue(mockError);
await expect(client.createPost({
content: '',
platforms: ['instagram']
})).rejects.toThrow('Invalid content');
});
});API Testing Tools
Postman Collection
Import our Postman collection for interactive API testing:
Insomnia Workspace
Download our Insomnia workspace for REST client testing:
Debugging Common Issues
Authentication Errors (401)
Symptoms: Getting 401 Unauthorized responses
Common Causes:
- Invalid or expired API key
- Missing Authorization header
- Incorrect Bearer token format
Solution:
Authorization: Bearer YOUR_API_KEY
Content-Type: application/jsonRate Limiting (429)
Symptoms: 429 Too Many Requests responses
Solution: Implement exponential backoff
const retryAfter = response.headers['retry-after'];
await delay(retryAfter * 1000);Validation Errors (400)
Symptoms: 400 Bad Request with validation messages
Common Issues:
- Missing required fields
- Invalid data formats
- File size/type restrictions
Test Environment Setup
Sandbox Mode
Use sandbox mode to test without actually posting to social media platforms.
# .env.test ZYHAP_API_KEY=test_key_sandbox ZYHAP_BASE_URL=https://sandbox-api.zyhap.com ZYHAP_SANDBOX_MODE=true
Monitoring & Logging
Request/Response Logging
Log all API requests and responses for debugging (excluding sensitive data).
Error Tracking
Use tools like Sentry or Bugsnag to track API errors in production.
Performance Monitoring
Monitor API response times and set up alerts for performance degradation.
const logger = require('winston');
// Log API requests
axios.interceptors.request.use(config => {
logger.info('API Request', {
method: config.method,
url: config.url,
headers: { ...config.headers, authorization: '[REDACTED]' }
});
return config;
});
// Log API responses
axios.interceptors.response.use(
response => {
logger.info('API Response', {
status: response.status,
url: response.config.url,
responseTime: Date.now() - response.config.startTime
});
return response;
},
error => {
logger.error('API Error', {
status: error.response?.status,
message: error.response?.data?.message,
url: error.config?.url
});
return Promise.reject(error);
}
);✅ Testing Checklist
Before Going Live
- ☐ Test with sandbox API keys
- ☐ Verify error handling
- ☐ Test rate limiting scenarios
- ☐ Validate all data formats
- ☐ Test webhook endpoints
Production Ready
- ☐ Monitor API usage
- ☐ Set up error alerts
- ☐ Implement retry logic
- ☐ Test failover scenarios
- ☐ Document your integration