✅ Recommended practices
1. Manage access_token efficiently
It’s crucial to manage the access_token obtained from /auth efficiently to avoid rate limiting and IP bans. This is especially important when processing high volumes of documents.
Best practice: Store the access_token for 60 minutes and reuse it for all subsequent document issuances. Do not request a new token for each document creation.
Example: Token management
Response:
Implementation recommendation:
- Cache the
access_tokenwith a TTL of 60 minutes (3600 seconds) - Reuse the same token for all API calls within this period
- Only request a new token when the current one expires or is about to expire
- This prevents excessive authentication requests that could trigger rate limiting on your IP
2. IP whitelisting (for high volumes)
If your system processes more than 5 documents per second, you should provide us your IP addresses for whitelisting to prevent potential blocks.
Why this is important:
- High-volume traffic may trigger security measures
- Whitelisting ensures your IPs are not blocked
- Contact support to add your IPs to the whitelist
Action required: Contact your sales representative with:
- Your production IP addresses
- Expected volume (documents per second)
- Account information
3. Respect rate limits and handle 429 errors
Always respect the rate limits defined in your contract. If you exceed the rate limit, you will receive a 429 Too Many Requests error.
Best practices:
- Monitor your request rate: Do not exceed the rate limit specified in your contract
- Implement exponential backoff: When receiving a
429error, wait at least 30 seconds before retrying - Handle retries gracefully: Implement a retry mechanism with proper delays
Example: Handling 429 errors
Implementation recommendation:
4. Prevent duplicates using reference field
To avoid duplicate document creation when retrying requests, use the reference field with reference_type 802 (Nota de pedido) containing your internal transaction ID.
Sending a document with reference
When creating a document, include the references array with your internal transaction ID:
Key points:
reference_type: 802 (Nota de pedido) - used for non-tax document referencesreference_number: Your internal transaction ID (e.g., “TXN-2025-001234”)reference_code: 5 (reference to other non-tax documents)
Searching documents by reference
To check if a document with a specific reference already exists, use the GET /documents endpoint with query parameters:
Endpoint: GET https://api-billing.koywe.com/V1/documents
Query parameters:
reference_type:802(to filter by reference type)reference_number: Your internal transaction ID (e.g.,TXN-2025-001234)
Example request:
Example response:
Implementation recommendation when you are retrying to issue:
- Before creating a document, check if a document with your internal transaction ID already exists (DO NOT include this validation on every common issuance or you will face performance issues)
- If found, use the existing
document_idinstead of creating a duplicate - If not found, proceed with document creation including the reference field
- This ensures idempotency and prevents duplicate documents
Summary: Follow these best practices for reliable document issuance: cache your access token, whitelist your IPs (if processing high volumes), respect rate limits with proper backoff, and use reference fields to prevent duplicates.