Skip to content

Example: Worker sample

Context

  • Language: Python (pseudo-implementation)
  • Permissions: messages:read, messages:ack

Goal

Poll continuously, process messages, ack on success, and retry on transient failures.

Sample code

import os
import time
import requests

BASE_URL = os.getenv("IRON_BASE_URL", "https://sandbox.api.iron.example")
API_KEY = os.environ["IRON_API_KEY"]

session = requests.Session()
session.headers.update({"Authorization": f"Bearer {API_KEY}"})

while True:
    r = session.get(
        f"{BASE_URL}/v1/messages:poll",
        params={"max": 10, "visibility_timeout_seconds": 60},
        timeout=30,
    )
    r.raise_for_status()

    messages = r.json().get("messages", [])
    if not messages:
        time.sleep(1)
        continue

    for m in messages:
        # Idempotency: ensure your handler is safe to run twice.
        handle(m)
        ack = session.post(f"{BASE_URL}/v1/messages/{m['id']}:ack", timeout=30)
        ack.raise_for_status()

Expected result

  • The worker processes messages and they stop reappearing once acked.

Common failure modes

  • Ack after visibility timeout → duplicates (see Concepts)

Next: Getting Started · Reference · Changelog · Support