> ## Documentation Index
> Fetch the complete documentation index at: https://docs.heydollr.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Collect with Python

> Collect a payment via invoice (orders use the same flow) using the Dollr API with Python requests.

<Note>
  **Invoices vs orders:** These guides walk through **invoices** (formal billing, line items, publish). The collect flow is the same for **orders** — use `/v1/orders/*` instead of `/v1/invoices/*` and set `source_type: "ORDER"` on the checkout session. See [Orders](/api/orders) and [Parties & counterparties](/concepts/parties-and-counterparties).

  **Hosted checkout:** To skip party, invoice, session, and execute steps entirely, see [Hosted checkout](/guides/hosted-checkout).
</Note>

End-to-end **invoice collection** with `requests`. For cURL and Node.js in parallel, see [Quick Start](/quickstart).

<Note>
  **API Reference:** [Obtain token](/api-reference/jwt/client-obtain-token) · [Collect](/api-reference/executions/collect)
</Note>

## Prerequisites

* Python 3.9+
* `pip install requests`
* API credentials from the merchant portal (server-side only)

## Steps

<Steps>
  <Step title="Authenticate">
    ```python theme={null}
    import requests

    BASE_URL = "https://api.heydollr.app"

    res = requests.post(f"{BASE_URL}/v1/jwt/client/obtain/token", json={
        "client_id":     "YOUR_CLIENT_ID",
        "client_secret": "YOUR_CLIENT_SECRET",
    })
    token = res.json()["access_token"]
    headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
    ```
  </Step>

  <Step title="Party and counterparty">
    ```python theme={null}
    party = requests.post(f"{BASE_URL}/v1/parties/create", headers=headers, json={
        "fullname": "Amara Kamara", "phone": "231771234567",
        "email": "amara@example.com", "country_code": "LR",
    }).json()

    cp = requests.post(f"{BASE_URL}/v1/counterparties/create", headers=headers, json={
        "relationship_type": "CUSTOMER", "party_id": party["id"],
    }).json()
    ```
  </Step>

  <Step title="Invoice → publish">
    ```python theme={null}
    invoice = requests.post(f"{BASE_URL}/v1/invoices/create", headers=headers, json={
        "counterparty_id": cp["id"], "currency": "USD",
        "note": "Consulting services", "fee_bearer": "PAYER", "as_payment_link": True,
    }).json()

    requests.post(
        f"{BASE_URL}/v1/invoices/{invoice['id']}/items/add",
        headers=headers,
        json={"name": "Strategy Session", "currency": "USD", "qty": 1, "amount": 250.00},
    )
    requests.put(f"{BASE_URL}/v1/invoices/publish/{invoice['id']}", headers=headers)
    ```
  </Step>

  <Step title="Execute collection">
    ```python theme={null}
    import uuid

    session = requests.post(f"{BASE_URL}/v1/sessions/checkout", headers=headers, json={
        "source_id": invoice["id"], "source_type": "INVOICE",
    }).json()

    account = requests.post(
        f"{BASE_URL}/v1/payment-accounts/create",
        headers=headers, params={"operation_type": "COLLECTION"},
        json={
            "account_name": "Amara MTN Wallet",
            "provider": "MTN_MOMO_LBR", "method": "MTN_MOMO_LBR",
            "party_id": party["id"], "country_code": "LR",
            "insensitive_account_number": "231771234567",
        },
    ).json()

    reference_id = str(uuid.uuid4())
    requests.post(f"{BASE_URL}/v1/executions/collection", headers=headers, json={
        "session_id": str(session["id"]),
        "payment_account_id": str(account["id"]),
        "currency": "USD",
        "reference_id": reference_id,
    })
    ```
  </Step>

  <Step title="Poll status">
    ```python theme={null}
    status = requests.get(
        f"{BASE_URL}/v1/status/collection/{reference_id}",
        headers=headers,
    ).json()
    ```
  </Step>
</Steps>

## Try it yourself

<CardGroup cols={3}>
  <Card title="Hosted checkout" icon="window-maximize" href="/guides/hosted-checkout">
    Mobile money and card on a Dollr-hosted page — fastest path.
  </Card>

  <Card title="Orders" icon="box" href="/api/orders">
    Same collect flow with `source_type: ORDER` and `/v1/orders/*`.
  </Card>

  <Card title="Direct checkout" icon="cart-shopping" href="/guides/collect-via-checkout">
    One API call to create source — API-embedded flow.
  </Card>
</CardGroup>

## Related

* [Hosted checkout](/guides/hosted-checkout) · [Orders](/api/orders)
* [Collect via checkout](/guides/collect-via-checkout)
* [Choose your integration](/guides/choose-integration)
* [Error handling](/guides/error-handling)
* [Sessions & executions](/concepts/sessions-and-executions)
