> ## 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 .NET

> Collect a payment via invoice (orders use the same flow) using the Dollr API from C# and .NET.

<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 `HttpClient` and `System.Text.Json` (.NET 6+).

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

## Prerequisites

* .NET 6 or later
* Register a typed `HttpClient` or use `IHttpClientFactory`
* Store secrets in User Secrets, Azure Key Vault, or environment variables — not `appsettings.json` in source control

## Steps

<Steps>
  <Step title="Token request">
    ```csharp theme={null}
    using System.Net.Http.Json;
    using System.Text.Json;

    var baseUrl = "https://api.heydollr.app";
    using var http = new HttpClient { BaseAddress = new Uri(baseUrl) };

    var tokenRes = await http.PostAsJsonAsync("/v1/jwt/client/obtain/token", new
    {
        client_id = Environment.GetEnvironmentVariable("DOLLR_CLIENT_ID"),
        client_secret = Environment.GetEnvironmentVariable("DOLLR_CLIENT_SECRET"),
    });
    tokenRes.EnsureSuccessStatusCode();
    using var doc = await JsonDocument.ParseAsync(await tokenRes.Content.ReadAsStreamAsync());
    var bearer = doc.RootElement.GetProperty("access_token").GetString();

    http.DefaultRequestHeaders.Authorization =
        new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", bearer);
    ```
  </Step>

  <Step title="Invoice flow">
    ```csharp theme={null}
    var party = await http.PostAsJsonAsync("/v1/parties/create", new
    {
        fullname = "Amara Kamara",
        phone = "231771234567",
        country_code = "LR",
    });
    // Parse party id, create counterparty, create invoice, add item, PUT publish
    ```
  </Step>

  <Step title="Execute">
    ```csharp theme={null}
    var referenceId = Guid.NewGuid().ToString();

    var execution = await http.PostAsJsonAsync("/v1/executions/collection", new
    {
        session_id = sessionId,
        payment_account_id = paymentAccountId,
        currency = "USD",
        reference_id = referenceId,
    });
    ```

    Save `referenceId` to your database **before** awaiting the response.
  </Step>

  <Step title="Poll status">
    ```csharp theme={null}
    var statusRes = await http.GetAsync($"/v1/status/collection/{referenceId}");
    var statusJson = await statusRes.Content.ReadFromJsonAsync<JsonElement>();
    ```
  </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)
