Démarrage rapide Python - Rendu de code-barres

Ce guide montre comment appeler le point de terminaison de génération de code-barres en Python à l'aide de httpx ou de requests.

Prérequis

Utilisation de httpx (recommandé)

Installez la dépendance :

pip install httpx
import os
import httpx

API_BASE = "https://api.product-data-api.com"


def render_barcode(payload: dict, api_key: str) -> dict:
    """
    Génère un code-barres et renvoie les métadonnées de réponse.
    """
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    url = f"{API_BASE}/api/v1/barcodes/render"

    with httpx.Client() as client:
        r = client.post(url, json=payload, headers=headers)

    r.raise_for_status()
    return r.json()


if __name__ == "__main__":
    api_key = os.environ["PRODUCT_DATA_API_KEY"]

    request_payload = {
        "type": "ean13",
        "data": "4006381333931",
        "format": "png",
        "width": 200,
        "height": 100,
        "metadata": {
            "copyright": "Copyright 2026 open4goods"
        }
    }

    response = render_barcode(request_payload, api_key)
    print(f"URL de l'image : {response['assetUrl']}")
    print(f"Expire le : {response['expiresAt']}")

Utilisation de requests

Installez la dépendance :

pip install requests
import os
import requests

API_BASE = "https://api.product-data-api.com"


def render_barcode(payload: dict, api_key: str) -> dict:
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    url = f"{API_BASE}/api/v1/barcodes/render"

    r = requests.post(url, json=payload, headers=headers)
    r.raise_for_status()
    return r.json()

Liens rapides