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

# Get margin

> Get cost and revenue, bucketed by period and optionally grouped by customer, alongside the resulting margin and risk status.

<Note>
  Margin is computed on revenue-linked projects (the default project type). On an internal project (tracked by key metrics, not revenue), this route returns a 400: use [Get performance](/api/performance#get-performance) instead.
</Note>

<CodeGroup>
  ```python Python theme={null}
  import weflayr

  client = weflayr.client(api_key="wf-...")
  ```

  ```js Node.js theme={null}
  const weflayr = require('weflayr');

  const client = weflayr.client({ apiKey: 'wf-...' });
  ```

  ```bash cURL theme={null}
  # Passed as a Bearer token on every request
  export WEFLAYR_API_KEY="wf-..."
  ```
</CodeGroup>

## Get margin

<CodeGroup>
  ```python Python theme={null}
  from weflayr.openapi.models import MarginInput

  client.get_margin(body=MarginInput.from_dict({
      "date_from": "2026-06-01",
      "aggregate": "week",
      "group_by": "customer",
      "customers": ["c_123", "c_456"],
  }))
  ```

  ```js Node.js theme={null}
  await weflayr.api.getMargin({
    client,
    throwOnError: true,
    body: {
      dateFrom: '2026-06-01',
      aggregate: 'week',
      groupBy: 'customer',
      customers: ['c_123', 'c_456'],
    },
  });
  ```

  ```bash cURL theme={null}
  curl -X POST https://app.weflayr.com/api/margin/ \
    -H "Authorization: Bearer $WEFLAYR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "date_from": "2026-06-01",
      "aggregate": "week",
      "group_by": "customer",
      "customers": ["c_123", "c_456"]
    }'
  ```
</CodeGroup>

**Output:**

```json theme={null}
{
  "rows": [
    {"date_from": "2026-06-01", "date_to": "2026-06-07", "customer": "c_123", "revenue": 1200.5, "cost": 420.0, "margin": 780.5, "margin_pct": 65.02, "risk_status": "healthy"},
    {"date_from": "2026-06-01", "date_to": "2026-06-07", "customer": "c_456", "revenue": 800.0, "cost": 950.0, "margin": -150.0, "margin_pct": -18.75, "risk_status": "unprofitable"}
  ],
  "next_cursor": null,
  "has_more": false
}
```

`customer` is omitted when `group_by` isn't set.

**Input params:** passed in the request body.

<ParamField body="date_from" type="&#x22;YYYY-MM-DD&#x22;" required>
  Start of the period, inclusive.
</ParamField>

<ParamField body="date_to" type="&#x22;YYYY-MM-DD&#x22;">
  End of the period, inclusive. Defaults to today.
</ParamField>

<ParamField body="aggregate" type="&#x22;day&#x22; | &#x22;week&#x22; | &#x22;month&#x22; | &#x22;quarter&#x22; | &#x22;year&#x22; | &#x22;all&#x22;" default="&#x22;day&#x22;">
  One row per period; `"all"` collapses the whole date range into a single row (per `group_by`, if set).
</ParamField>

<ParamField body="group_by" type="&#x22;customer&#x22;">
  Split each period into one row per customer. Omit for one row per period.
</ParamField>

<ParamField body="customers" type="list[string]">
  Restrict to these customers. Omit for every customer's. See [Get customers](/api/metadata#get-customers).
</ParamField>

<ParamField body="filter_customers_based_on_features" type="list[string]">
  Restrict to customers who used any of these feature tags in the period (i.e. if a customer did not use the feature, all its rows are removed).
</ParamField>

<ParamField body="apply_credits" type="boolean" default="false">
  Apply [free AI credits](/configure/free-ai-credits) discounts to `cost`.
</ParamField>

<ParamField body="limit" type="integer" default="100">
  Max number of rows to fetch. Max `1000`.
</ParamField>

<ParamField body="cursor" type="string">
  The route is paginated: pass the previous page's `next_cursor` to fetch the next page. PS: `has_more` from the output says whether another page exists.
</ParamField>
