Best Practices
Overview
Follow these best practices to build production-ready integrations that are secure, reliable, and performant.
- Security: Protect credentials and sensitive data.
- Error Handling: Handle failures gracefully with retries.
- Performance: Optimize API usage and respect rate limits.
- Reliability: Build resilient integrations that handle edge cases.
Authentication & Security
Credential Management
Never hardcode credentials
Don’t do this:
Do this instead:
- Use environment variables or secure vault services
- Never commit credentials to version control
- Add
.envto your.gitignorefile
Rotate credentials regularly
Recommended rotation schedule:
- API tokens: Every 90 days
- OAuth2 access tokens: Refresh proactively (every 25 days)
- Immediately rotate if compromise is suspected
Why rotate?
- Employees leave
- Credentials can be accidentally exposed
- Security vulnerabilities can be discovered
Use the least privilege principle
Only request the scopes and permissions you absolutely need:
Too broad:
Appropriate:
- Create separate tokens for different integrations
- Review and remove unused scopes
- Use organization tokens only when necessary
Always use HTTPS
All API requests must use HTTPS:
Incorrect:
Correct:
- HTTPS encrypts data in transit
- Protects against man-in-the-middle attacks
- Required by Deel API (HTTP requests will fail)
Webhook Security
When receiving webhooks, always verify the signature:
Error Handling & Retries
Implement Exponential Backoff
Retry failed requests with exponential backoff to avoid overwhelming the API:
Handle Specific Error Codes
Different errors require different handling strategies:
Validate Request Data
Always validate data before sending to the API:
Rate Limiting
Deel enforces a rate limit of 5 requests per second per organization. This limit is shared across all API tokens in your organization.
Important: Rate limits are organization-wide and Deel does not return rate limit headers. Proactive rate limiting through request queuing is essential.
Key Strategies
- Request Queuing: Always implement request queuing to stay within limits.
- Space Out Requests: Avoid bursts—spread requests over time.
- Cache Responses: Reduce unnecessary API calls.
- Centralize Requests: Coordinate all API calls across your organization.
Quick example:
See the complete Rate Limits documentation for detailed strategies, code examples, and troubleshooting.
Idempotency
Use idempotency keys for POST and PATCH requests to safely retry without creating duplicates.
Idempotency keys prevent duplicate resources when retrying failed requests. Responses are cached for 24 hours.
Quick example:
Key points:
- Use UUID v4 for idempotency keys
- Reuse the same key when retrying
- Only successful responses (2xx) are cached
- Keys are valid for 24 hours
See the complete Idempotency documentation for detailed implementation, scenarios, and best practices.
Data Handling
Sanitize and Validate Input
Validate user input
Always validate and sanitize data from users:
Handle timezone conversions
Always use UTC for dates and times:
Handle pagination efficiently
For large datasets, use pagination properly:
Testing
Test in Sandbox First
Test error scenarios
Don’t just test happy paths. Test:
- Invalid authentication
- Missing required fields
- Rate limit handling
- Network failures
- Webhook signature verification
Monitoring & Logging
Log Important Events
Implement structured logging for debugging and monitoring:
What to log:
- API request/response metadata (not full bodies with sensitive data)
- Error conditions and stack traces
- Authentication failures
- Rate limit warnings
- Webhook deliveries
What NOT to log:
- API keys or tokens
- Sensitive personal data
- Full request/response bodies (unless sanitized)
Set Up Alerts
Monitor your integration health:
- Error Rate Alerts: Alert when error rate exceeds threshold (e.g., >5% of requests)
- Rate Limit Warnings: Alert when approaching rate limits (e.g., 80% usage)
- Webhook Failures: Alert on webhook delivery failures or signature mismatches
- Response Time: Alert on slow API responses (e.g., >2 seconds average)
Performance Optimization
Use connection pooling
Reuse HTTP connections for better performance:
Implement request timeouts
Always set reasonable timeouts:
Minimize payload size
Only request the fields you need:
Use async/concurrent requests
When fetching multiple independent resources:
Compliance & Privacy
Handle PII appropriately
Personal Identifiable Information (PII) requires special care:
- Only collect necessary PII
- Encrypt PII at rest and in transit
- Follow data retention policies
- Implement right to deletion
- Document data flows
Comply with data regulations
Ensure compliance with relevant regulations:
- GDPR (Europe): Data protection and privacy
- CCPA (California): Consumer privacy rights
- SOC 2: Information security standards
- Industry-specific regulations
Audit trail
Maintain audit logs for compliance:
Summary Checklist
Before deploying to production, ensure you’ve implemented:
Security Checklist
- API keys stored in environment variables
- HTTPS used for all requests
- Webhook signatures verified
- Least privilege scopes requested
- Credential rotation schedule in place
- Secrets never committed to version control
Reliability Checklist
- Exponential backoff retry logic
- Idempotency keys used for mutations
- Error handling for all API calls
- Request timeouts configured
- Input validation implemented
- Edge cases tested
Performance Checklist
- Rate limits respected
- Request queuing implemented
- Appropriate caching strategy
- Connection pooling enabled
- Pagination handled efficiently
- Concurrent requests where possible
Monitoring Checklist
- Structured logging implemented
- Error tracking configured
- Rate limit monitoring
- Alerts set up for failures
- Webhook delivery monitoring
- Performance metrics tracked