Skip to main content

Robust Retries for Idempotent Identity Verification API Calls in Python

Building resilient systems requires careful handling of transient API failures. This guide explores implementing robust retry mechanisms for idempotent identity verification API calls in Python, focusing on strategies like.

Robust Retries for Idempotent Identity Verification API Calls in Python

Contents

In this guide

Jump to the part you need.

Idempotency is KeyDesign your API calls to be idempotent, meaning a request can be made multiple times without changing the result beyond the initial execution, preventing unintended side effects during retries.

Exponential Backoff is EssentialImplement an exponential backoff strategy with jitter to avoid overwhelming the API with retries and to space out attempts effectively, improving the chances of success as temporary issues resolve.

Idempotency Keys for ConsistencyUtilize unique idempotency keys in your API requests to ensure that even if a request is received multiple times due to retries, the underlying operation is processed only once, safeguarding data integrity.

Didit Simplifies ResilienceDidit's AI-native identity verification platform is built with idempotency in mind, offering a developer-first approach with clean APIs that inherently support retry mechanisms and idempotency keys, alongside Core KYC and a modular architecture.

In the world of identity verification, reliability and data integrity are paramount. When integrating with external APIs, especially for critical operations like ID Verification, network glitches, temporary service outages, or rate limiting can lead to failed requests. A well-designed retry mechanism is crucial to build robust applications that can withstand these transient failures without compromising data consistency or user experience. This article delves into building a robust retry mechanism for idempotent identity verification API calls in Python, ensuring your system is resilient and reliable.

Understanding Idempotency in API Calls

Before diving into retry strategies, it's vital to grasp the concept of idempotency. An idempotent operation is one that can be applied multiple times without changing the result beyond the initial application. For instance, setting a user's status to 'verified' is idempotent; doing it once or ten times yields the same final state. In contrast, an operation like 'create new user' is typically not idempotent, as executing it multiple times would create multiple users.

For identity verification, many operations, especially those involving submitting a document for Didit's ID Verification or initiating a Passive & Active Liveness check, should ideally be designed to be idempotent. This means if you submit the same verification request twice (perhaps due to a network timeout), the API should recognize it as a duplicate and process it only once, or return the result of the original processing, rather than initiating a new, redundant verification.

Didit's APIs are designed with idempotency in mind, allowing you to confidently retry requests. This is often achieved through the use of an idempotency_key in the request header or body, a unique identifier generated by your client for each distinct logical request. The API server uses this key to detect and ignore duplicate requests within a certain timeframe, ensuring that even if your retry mechanism fires, the core operation is executed only once.

The Need for Robust Retry Mechanisms

Why are retries so important? Imagine a scenario where a user submits their ID for verification. A network hiccup occurs, and your application doesn't receive a response. Without a retry mechanism, the user might be left in limbo, or your system might incorrectly assume the verification failed. A retry mechanism automatically re-sends the request, increasing the likelihood of success once the temporary issue is resolved. However, a naive retry strategy can exacerbate problems by:

  • Overwhelming an already struggling API with a flood of repeated requests.
  • Hitting rate limits more quickly.
  • Creating duplicate records or unintended side effects if the API isn't idempotent.

Therefore, a robust strategy is needed.

Implementing Exponential Backoff with Jitter

The cornerstone of a robust retry mechanism is exponential backoff with jitter. This strategy involves:

  1. Exponential Backoff: Instead of retrying immediately, wait for progressively longer periods between retries (e.g., 1 second, then 2 seconds, then 4 seconds, then 8 seconds). This gives the API server time to recover.
  2. Jitter: Add a small, random delay to each backoff period. This prevents all clients from retrying at the exact same time, which could create a thundering herd problem and overwhelm the service again.

Let's look at a Python example using the requests library and a custom retry decorator:


import requests
import time
import random
from functools import wraps

def retry_with_exponential_backoff(max_retries=5, initial_delay=1, factor=2, jitter=0.1):
    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            delay = initial_delay
            for i in range(max_retries):
                try:
                    return func(*args, **kwargs)
                except requests.exceptions.RequestException as e:
                    if i == max_retries - 1:
                        raise # Re-raise the last exception if all retries fail
                    print(f"Request failed: {e}. Retrying in {delay:.2f} seconds...")
                    time.sleep(delay + (random.random() * jitter * delay))
                    delay *= factor
        return wrapper
    return decorator

# Example usage with a Didit API call
@retry_with_exponential_backoff(max_retries=3, initial_delay=0.5)
def create_didit_session(api_key, workflow_id, vendor_data):
    url = "https://verification.didit.me/v3/session/"
    headers = {
        "x-api-key": api_key,
        "Content-Type": "application/json"
    }
    data = {
        "workflow_id": workflow_id,
        "vendor_data": vendor_data,
        "callback": "https://yourapp.com/didit/webhook/handler"
    }
    response = requests.post(url, headers=headers, json=data, timeout=10)
    response.raise_for_status() # Raise an HTTPError for bad responses (4xx or 5xx)
    return response.json()

# --- In your application code ---
# try:
#     session_data = create_didit_session(
#         api_key="YOUR_DIDIT_API_KEY",
#         workflow_id="YOUR_WORKFLOW_ID",
#         vendor_data="user_abc_123"
#     )
#     print(f"Didit Session created: {session_data['url']}")
# except requests.exceptions.RequestException as e:
#     print(f"Failed to create Didit session after multiple retries: {e}")

This decorator can be applied to any function making an API call, providing a flexible and reusable solution. For critical operations like AML Screening or NFC Verification, such a robust retry mechanism is indispensable.

Leveraging Idempotency Keys for Data Consistency

While exponential backoff handles transient network issues, idempotency keys handle the potential for duplicate processing if a request successfully reaches the server but the response is lost. By adding a unique, client-generated idempotency key to each request, the Didit API can guarantee that the operation is performed only once, even if the request is retried multiple times. This is particularly important for financial transactions or state-changing operations.

When making a POST request to create a session for Didit's ID Verification, you might include an idempotency_key in your request. If the first request times out, and you retry with the same key, Didit's system will recognize the key and return the result of the initial, successful processing rather than starting a new one. This prevents scenarios like accidentally triggering two separate verifications for the same user.

Handling Different Error Types and Status Codes

Not all errors warrant a retry. For example, a 400 Bad Request or 401 Unauthorized indicates a client-side error that won't be resolved by retrying. Your retry mechanism should ideally distinguish between transient errors (e.g., 429 Too Many Requests, 5xx Server Errors, network timeouts) and permanent errors. The requests.exceptions.RequestException in the example above catches network-related issues and server errors fairly broadly. For more granular control, you could inspect response.status_code within the try block before raising HTTPError.

How Didit Helps

Didit, as an AI-native, developer-first identity platform, is built from the ground up to support resilient integrations. Our modular architecture and clean APIs inherently simplify the implementation of robust retry mechanisms. Didit's platform embraces idempotency, ensuring that operations like initiating an ID Verification, performing a 1:1 Face Match, or conducting AML Screening are safe to retry. We achieve this by:

  • Idempotent API Design: Our APIs are crafted to handle repeated requests gracefully, often by supporting idempotency keys, which means your retry logic can be simpler and more reliable.
  • Clear Error Codes: Didit provides explicit HTTP status codes and error messages, allowing you to precisely determine whether an error is transient and retryable or requires developer intervention.
  • Developer-First Experience: With an instant sandbox and comprehensive public documentation, developers can quickly integrate and test their retry mechanisms against Didit's services.
  • Core KYC: You can start building and testing your robust integrations, including retry logic, using a free Didit account, making it cost-effective to ensure reliability from day one.
  • Orchestrated Workflows: Our no-code engine for KYC allows you to define complex verification flows. When using verification links created via API, the underlying session creation is designed for resilience, complementing your client-side retry strategies.

By leveraging Didit's platform, you spend less time worrying about the nuances of API communication failures and more time focusing on building secure and compliant identity verification experiences for your users.

Ready to Get Started?

Ready to see Didit in action? Get a free demo today.

Start verifying identities for free with Didit's free tier.

Try it

Try this in the sandbox

Run the check this guide describes, free.

ID Verification

The product this post is about, taken from its topic. Run it free in the sandbox.

$0.15 per checkStart free

Author

Alejandro Rosas

Co-founder and CTO

Alejandro Rosas

Alejandro Rosas

Co-founder and CTO

About Didit

Questions

Blog questions

Who writes the Didit blog?
The Didit team. Guides reviewed by Alberto Rosas, co-founder and CEO, carry his byline. The rest are published as Didit.
Which topics does the blog cover?
Eight topics: ID verification, business verification (KYB), AML, sanctions and compliance, fraud prevention, transaction monitoring and crypto, country guides, agents and developers, and product updates.
Is the blog available in my language?
Yes, in 15 languages: English, Spanish, French, German, Brazilian and European Portuguese, Catalan, Russian, Arabic, Chinese, Japanese, Hindi, Indonesian, Korean and Swahili. Not every post is translated, so each language shows the posts it has.
Where do I find product updates?
In the changelog at /changelog/ and under the Product updates topic.
Are these posts legal advice?
No. Regulation posts explain rules; your legal team decides how they apply.
How do I find posts on one topic?
Open one of the eight topics, or search the blog.
Are posts dated?
Yes. Every post shows when it was published and when it was last updated.
Do posts link to the docs?
Developer posts link to the matching page on docs.didit.me and to the API reference.
Is the blog the same as the docs?
No. The blog explains rules, markets and product changes for a general reader, while docs.didit.me holds the integration guides and the API reference. Developer posts link the matching docs page and the API reference they describe.

Compliance built in

Audited, certified and attested.

We hold the certifications and build each country's rules into your flows, so you can launch anywhere without worrying. Security and Compliance center

  • Regulation encoded into your flows

    Tell us what you do and where. Didit lists the rules that apply to you, with sources, and builds the checks each country needs into one workflow.

  • Ready for AMLR

    Every requirement of the new EU anti-money laundering rules, from checking customers to human review, is already covered by a Didit check.

  • Tested in Spain's Treasury sandbox

    Spain's Treasury published its supervised sandbox report on Didit's NFC plus active liveness onboarding; SEPBLAC took part and valued the added measures positively.

  • Audited, not self-declared

    iBeta Level 1 and Level 2. Liveness tested under ISO/IEC 30107-3.

Compliance assistant

Your obligations · 3 regions

EU · AMLRCustomer due diligence, human reviewCovered
Illinois, Texas, ColoradoExplicit biometric consentCovered
Germany · Youth protectionVerified adults only (JMStV)Covered
Workflow checked. Every obligation is met
  • SOC 2 Type 2
  • ISO 27001
  • iBeta Level 1 and Level 2
  • EU regulator attestation
  • FSM youth protection
  • GDPR
  • EBA remote onboarding
  • MiCA compatible
  • FIDO Alliance member
  • AMLREU AML Regulation ready

Integration · Didit MCP

Run Didit from your own AI agent

Connect Didit to Claude, Cursor, VS Code or ChatGPT and just ask in plain words. Your agent can do anything you can do in the console, with the same permissions you have.

Add to ClaudeChatGPT
Didit Academy · Lesson 04 · Use Didit with MCP
  • Build workflowsDescribe the checks you need. The agent creates and publishes the flow.
  • Run investigationsAsk why a session was declined, review cases, search people and companies.
  • Change rulesCreate payment rules, test them on past data and switch them on.
  • Pull reportsAsk for numbers, exports or the audit log.

Verify your first user today.

Start free

Free sandbox. Credit applies to work-email sign-ups.