API integration means getting your own software to talk to another system, such as a payment gateway, e-invoicing, shipping, ERP or CRM, through an application programming interface (API). A good integration behaves correctly when the other system slows down, returns an error or receives the same request twice. That comes down to a set of basics: protecting credentials, timeouts and retries, idempotency, validating external data, verifying webhooks and monitoring. I've worked on payment and e-invoice integrations, and the topics below are common in that kind of work.
Before You Start an API Integration
Before writing any code, get to know the other system. Get answers to these questions from the documentation and the provider:
- How does authentication work: API key, OAuth, signed requests?
- Is there a test environment, and does it behave like production?
- What's the rate limit, and what comes back when you exceed it?
- Are the error codes documented? Which errors can be retried?
- Does the provider notify you of events through webhooks, or do you have to poll?
The answers shape most of the integration's design. If the provider publishes an OpenAPI document, most of the answers are in it; I explain how to use one in the post on OpenAPI documentation. For language model APIs, see the AI API integration guide, and for live data streams, WebSocket vs SSE. When a system has no test environment, plan the first live trials separately.
1. Protect Your Credentials
API keys and secrets shouldn't be written into the code or committed to version control. Keep them in environment variables or a secrets manager. Beyond that:
- Where possible, use separate keys with only the permissions they need. A reporting job that only reads data shouldn't be able to issue refunds.
- Keep test and production keys separate.
- Have a process for rotating a key quickly if it leaks.
- Secret keys should never reach the browser or a mobile app. Make external API calls through your server.
2. Timeouts and Retries
The other system won't always answer quickly. A request with no timeout can leave your application waiting for minutes. Put a sensible timeout on every external call.
Temporary errors call for retries, but they need care. AWS's article on timeouts, retries and backoff with jitter (opens in a new tab) explains that the most common pattern is exponential backoff, where the wait time grows exponentially after each attempt, and that adding random jitter keeps all clients from retrying at the same moment.
- Retry only temporary errors: timeouts, connection errors, 502, 503, 504 or 429.
- Retry non-idempotent requests, such as creating a payment, only with an idempotency key. Otherwise the operation may run twice.
- Retrying errors in the request itself, such as 400 or 422, won't help.
- Put an upper limit on the number of attempts.
When you hit a rate limit, the server returns a 429 status code. RFC 6585 (opens in a new tab) defines that this response may include a Retry-After header saying how long to wait. If it's there, set your wait time by it.
3. Idempotency: Never Doing the Same Thing Twice
You sent a payment request and it timed out. Was the payment taken or not? If you retry, you might charge the customer twice. Idempotency means that sending the same request more than once has the same effect as sending it once.
On the HTTP side, RFC 9110 (opens in a new tab) defines PUT, DELETE and the safe methods as idempotent. POST isn't, so operations like creating a payment or an order need a separate mechanism. The common solution is an idempotency key. According to Stripe's documentation (opens in a new tab), the client generates a unique key and adds it to the request, and the server uses that key to recognize retries of the same request and avoid performing the operation twice.
If your provider supports a key like this, use it. If not, give every operation a unique reference on your side and check the operation's status before retrying.
4. Don't Blindly Trust Data From the Other System
The tenth item on the OWASP API Security Top 10 2023 (opens in a new tab) is unsafe consumption of APIs. OWASP's page for that item (opens in a new tab) notes that developers tend to trust data received from third-party APIs more than user input.
- Validate that the response has the structure you expect. A missing field or a field of a different type shouldn't cause errors elsewhere in your app.
- Treat text from an external system like user input when you write it to a page or add it to a query.
- Be careful with responses that contain redirects or file URLs, and don't follow them without validating.
5. Verify Webhooks
Many providers notify you through webhooks when a payment completes or an invoice is approved. Since your webhook URL is open to the internet, you need to verify with a signature that each request really comes from the provider. The same event can arrive more than once, so the webhook handler has to be idempotent too.
6. Logging, Monitoring and Alerts
When an integration quietly breaks, the customer shouldn't be the one to notice.
- Log the duration, status code and operation reference of every external call. Don't write sensitive fields like card details or personal data to the logs.
- Get alerted when the error rate or latency crosses a threshold.
- Keep failed operations in a queue so they aren't lost and can be reprocessed.
7. Abstract the Integration
Instead of scattering external API calls across your code, gather them in a single layer. Then when the provider changes or a second one is added, only that layer changes.
8. Test the Integration
Code that depends on an external system is hard to test, because the other side isn't always reachable and you can't produce every scenario. A two-layer approach helps. In unit tests, use a fake client that mimics the external API and try cases like timeouts, 429, 500 and unexpected responses one by one. Then run a real end-to-end flow in the provider's test environment. After going live, watch the first operations closely. Confirming the flow with a small test transaction is a good way to catch problems before they grow.
Common Mistakes
- Not setting timeouts.
- Blindly retrying every error and putting even more load on the other system.
- Not thinking about idempotency for operations like payments.
- Putting the API key in the code or on the client side.
- Assuming that what works in the test environment will work in production.
Frequently Asked Questions
Should I Use REST or GraphQL?
You work with whatever approach the system you're integrating with offers. If you're designing your own API, I compare the two in REST vs GraphQL.
How Long Does an Integration Take?
It depends on the quality of the documentation, whether there's a test environment and how complex the business rules are. The way to reduce uncertainty is to review the documentation before starting and confirm authentication works with a small test call.
What If the Provider Changes Its API?
Providers update their APIs over time and retire old versions. Follow the provider's announcements, state the API version you use explicitly in your code and, if you've gathered the integration into one layer, make the update there.
What Should I Watch for in an e-Invoice Integration?
e-Invoicing in Turkey is its own topic, with its own regulations and private integrators.
If you'd like to integrate your system with a payment, e-invoicing or other service, see the SaaS and Microservice Development service or write to me through the contact page.



