# Read business metrics

Use this guide to build a report for one organization and a clear date range. Decide which measures and units the
report needs before requesting data. A one-time local report can use CLI browser approval. A scheduled report
should use an API key with only the read scopes it actually needs.

Use the [generated OpenAPI contract](https://themochi.app/docs/api/openapi.json) for exact date parameters and result schemas.

For a member-bound connection, `revenue:read` requires a Creator, Manager, or Finance role. Organization policy may
add more restrictions.

## Define the report first

1. Name the organization and exact inclusive `date_from`/`date_to` calendar dates for analytics and revenue.
2. Use ISO 8601 timestamps with offsets for booking `start_from`/`start_to`, then normalize presentation separately. Do not silently mix organization-local calendar dates and UTC timestamps.
3. Choose only the required operations and scopes. A report that has no revenue section should not receive `revenue:read`.
4. Read the current OpenAPI response schemas before selecting fields.
5. Revenue integers are **minor currency units**, such as cents for a currency with two decimal places. Summary
   totals are in the organization's main currency, which the response names in `currency`, converted at the rate on
   each payment's day. Failed, pending and canceled charges are not counted. `conversion.unconverted_count` is how
   many payments have no exchange rate and are left out, and `conversion.pending` is true while the organization's
   main currency is changing. Individual transactions keep the currency they were paid in.
6. Treat conversion-rate numerators and denominators as part of the result, not just the displayed decimal.
7. Walk booking cursors to `null`; one page is not a complete period.

## Revenue summary request

These examples request one explicit calendar period. They print only the aggregate response; keep the raw result out of prompts and logs if it contains business-sensitive metrics.

### cURL

```bash
curl --fail-with-body --silent --show-error --get \
  "${MOCHI_BASE_URL}/v1/revenue/summary/" \
  --header "Authorization: Bearer ${MOCHI_API_KEY}" \
  --header "Accept: application/json" \
  --data-urlencode "date_from=${MOCHI_DATE_FROM}" \
  --data-urlencode "date_to=${MOCHI_DATE_TO}"
```

### Python

```python
import os

import requests

response = requests.get(
    f"{os.environ['MOCHI_BASE_URL']}/v1/revenue/summary/",
    headers={"Authorization": f"Bearer {os.environ['MOCHI_API_KEY']}"},
    params={"date_from": os.environ["MOCHI_DATE_FROM"], "date_to": os.environ["MOCHI_DATE_TO"]},
    timeout=30,
)
response.raise_for_status()
summary = response.json()
print({"period": summary["period"], "metrics_minor_units": summary["metrics"]})
```

### Node.js / TypeScript

```typescript
const baseUrl = process.env.MOCHI_BASE_URL ?? "https://api.themochi.app";
const url = new URL("/v1/revenue/summary/", baseUrl);
url.searchParams.set("date_from", process.env.MOCHI_DATE_FROM!);
url.searchParams.set("date_to", process.env.MOCHI_DATE_TO!);
const response = await fetch(url, {
  headers: { Authorization: `Bearer ${process.env.MOCHI_API_KEY}` },
  signal: AbortSignal.timeout(30_000),
});
if (!response.ok) throw new Error(`Mochi HTTP ${response.status}`);
const summary = (await response.json()) as { period: object; metrics: object };
console.log({ period: summary.period, metricsMinorUnits: summary.metrics });
```

### PHP

```php
<?php
$query = http_build_query([
    "date_from" => getenv("MOCHI_DATE_FROM"),
    "date_to" => getenv("MOCHI_DATE_TO"),
]);
$baseUrl = getenv("MOCHI_BASE_URL") ?: "https://api.themochi.app";
$handle = curl_init($baseUrl . "/v1/revenue/summary/?" . $query);
curl_setopt_array($handle, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTPHEADER => ["Authorization: Bearer " . getenv("MOCHI_API_KEY")],
]);
$body = curl_exec($handle);
$status = curl_getinfo($handle, CURLINFO_RESPONSE_CODE);
curl_close($handle);
if ($body === false || $status < 200 || $status >= 300) throw new RuntimeException("Mochi read failed");
$summary = json_decode($body, true, flags: JSON_THROW_ON_ERROR);
echo json_encode(["period" => $summary["period"], "metrics_minor_units" => $summary["metrics"]]);
```

## Verification and failure behavior

Verify that each response echoes the intended period, record `computed_at`, reconcile booking page counts, and compare a small aggregate against the Mochi product for the same organization and dates. Do not merge results whose periods or units differ.

Reads can be retried only as described in [Errors](https://themochi.app/docs/api/troubleshooting-and-safety/errors) and [Rate limits](https://themochi.app/docs/api/troubleshooting-and-safety/rate-limits). Bound retries, honor current server guidance, and retain only sanitized request metadata plus `X-Request-ID`. A failed report should remain failed or explicitly partial; never fill a missing metric with zero.
