> For the complete documentation index, see [llms.txt](https://docs.north.cloud/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.north.cloud/docs/features/coststreams/cost-units/api-integration.md). Agents can also search and read this documentation through its [MCP server](https://docs.north.cloud/docs/~gitbook/mcp).

# API Integration

## Cost Units API <a href="#cost-units-public-api" id="cost-units-public-api"></a>

North's Cost Units feature enables you to track and manage custom cost metrics within your cloud financial operations. With the Cost Units API, you can programmatically upload and retrieve cost unit data from outside the North UI, using secure API keys for authentication.

#### When to Use the API <a href="#when-to-use-the-public-api" id="when-to-use-the-public-api"></a>

Use the API when you want to:

* Automate recurring metric uploads from an internal data pipeline or warehouse
* Sync business metrics from a third-party system on a schedule
* Retrieve Cost Unit data programmatically for custom dashboards or reports
* Integrate metric uploads into CI/CD or ETL workflows

### Creating API Keys <a href="#creating-api-keys" id="creating-api-keys"></a>

Before you can use the API, you need to create an API key within the North application. API keys provide secure, programmatic access to your cost unit data without requiring interactive authentication.

#### Key Management in the North UI <a href="#key-management-in-the-north-ui" id="key-management-in-the-north-ui"></a>

To create an API key within the North application:

1. In the **Streams** tab in [app.north.cloud](http://app.north.cloud/), open a business unit and select **Cost Units**
2. Click **Create New**, then select **API Settings**
3. Click **Generate API Key**. If the business unit already has keys, click **Add**
4. Enter a name for the key, for example "Production Pipeline" or "Weekly Sync". Click **Next**
5. Optionally set an expiration date, up to 365 days from creation. Click **Next**
6. **Copy and save the API key immediately.** The full key is only shown once at creation time and cannot be retrieved later.
7. On the **Test** step, try the key with the code examples. Click **Done**

API keys use the format `north_api_{secret_hash}`.

#### Viewing API Keys <a href="#viewing-api-keys" id="viewing-api-keys"></a>

The API Keys section displays all active keys for your organization, including:

* Key name
* Date created
* Expiration date (if set)
* Last used timestamp

#### Revoking API Keys <a href="#revoking-api-keys" id="revoking-api-keys"></a>

To revoke a key, click the delete action next to the key you want to remove. Revoked keys are deactivated immediately, and API requests that use a revoked key are rejected.

**Important:** When you create a new API key, it will be displayed only once. Make sure to save it securely, as it cannot be retrieved later. If you lose an API key, you must create a new one and revoke the old key.

Each API key is scoped to your organization and provides access to all cost units within your account. The key is validated on every API request to ensure secure access to your data.

### API Endpoints <a href="#public-api-endpoints" id="public-api-endpoints"></a>

The API provides two endpoints for interacting with your cost units:

* **GET /public/cost-units** - Retrieve existing cost unit data
* **POST /public/cost-units** - Update cost unit data with new metrics

The base URL for both endpoints is `https://api.north.cloud`.

**Note:** The API supports updating existing cost units only. To create a Cost Unit, use **Manual entry** or **Import template** in North.

### Authentication <a href="#authentication" id="authentication"></a>

All API requests require authentication using an API key. Include your API key in the `Authorization` header of each request:

`Authorization: ApiKey north_api_{your_key}`

The API key is validated on every request. Requests with missing, invalid, expired, or revoked keys will receive a `403` response.

### Account IDs <a href="#account-ids" id="account-ids"></a>

Every Cost Units API request must include `north_account_id` as a query parameter. Each Cost Unit belongs to a Business Unit in a Cost Streams view. Send the ID of the view's cloud account and of each Cost Loop in the view, separated by commas. You can list the IDs in any order.

For the cloud account, use the ID of the account you connected to North. This is the AWS account ID, the GCP project ID, or the Azure billing account ID. Do not include linked accounts. You can find every account ID in [Settings](https://app.north.cloud/settings?tab=accounts), on the **Integrations** tab, in the **Account ID** column. The API validates that each ID belongs to the organization associated with your API key.

### GET /public/cost-units <a href="#get-public-cost-units" id="get-public-cost-units"></a>

Retrieve data from an existing cost unit.

`GET /public/cost-units`

#### Headers <a href="#headers" id="headers"></a>

| Header          | Required | Description                                                   |
| --------------- | -------- | ------------------------------------------------------------- |
| `Authorization` | Yes      | Your API key in the following format: `ApiKey <your-api-key>` |
| `Content-Type`  | No       | Content type (not required for GET requests)                  |

#### Query Parameters <a href="#query-parameters" id="query-parameters"></a>

| Parameter          | Required | Type   | Description                                                                                                                                   |
| ------------------ | -------- | ------ | --------------------------------------------------------------------------------------------------------------------------------------------- |
| `north_account_id` | Yes      | string | The ID of the view's cloud account and of each Cost Loop in the view, separated by commas. Each ID must belong to the API key's organization. |
| `cost_unit_id`     | Yes      | string | <p>Cost Unit ID. Copy it from the Cost Unit card in North.</p><p>(e.g., <code>bu-123:cu-456</code>)</p>                                       |

#### Example Request <a href="#example-request" id="example-request"></a>

{% code overflow="wrap" %}

```http
GET /public/cost-units?north_account_id=123456789012&cost_unit_id=bu-123:cu-456
Authorization: ApiKey north_api_AbCdEfGhIjKlMnOpQrStUvWx
```

{% endcode %}

#### Response <a href="#response" id="response"></a>

**Success (200 OK):**

```json
{
  "status": "success",
  "data": {
    "business_unit_id": "bu-123",
    "cost_unit_id": "cu-456",
    "view_id": "view-789",
    "status": "processed",
    "data": {
      "2024-01-01": { "metric": 100.5 },
      "2024-01-02": { "metric": 150.75 }
    },
    "errors": [],
    "warnings": [],
    "metadata": {
      "name": "Impressions",
      "type": "numerator",
      "granularity": "daily",
      "description": "Ad impressions"
    },
    "created_at": "2024-01-10T12:00:00",
    "updated_at": "2024-01-15T08:30:00"
  },
  "meta": {},
  "error": null,
  "message": null
}
```

**Error Responses:**

* **400 Bad Request:** Missing required parameters or invalid format
* **403 Forbidden:** The API key is missing, invalid, expired, or revoked, or does not have access to the requested resource. A 403 also means an account ID does not belong to your organization.
* **404 Not Found:** The business unit or cost unit was not found. The API also returns 404 when the list of account IDs does not match the accounts in the Cost Unit's view.
* **500 Internal Server Error:** An internal error occurred while processing your request

### POST /public/cost-units <a href="#post-public-cost-units" id="post-public-cost-units"></a>

Update an existing cost unit with new data points. This endpoint supports both merging new data with existing data and completely replacing existing data.

`POST /public/cost-units`

#### Headers <a href="#headers.1" id="headers.1"></a>

| Header          | Required | Description                       |
| --------------- | -------- | --------------------------------- |
| `Authorization` | Yes      | Your API key: `ApiKey {your_key}` |
| `Content-Type`  | Yes      | Must be `application/json`        |

#### Query Parameters <a href="#query-parameters.1" id="query-parameters.1"></a>

| Parameter          | Required | Type    | Description                                                                                                                                   |
| ------------------ | -------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| `north_account_id` | Yes      | string  | The ID of the view's cloud account and of each Cost Loop in the view, separated by commas. Each ID must belong to the API key's organization. |
| `replace`          | No       | boolean | When `true`, completely replaces existing data instead of merging. Default: `false`                                                           |

#### Request Body <a href="#request-body" id="request-body"></a>

| Field          | Required | Type   | Description                                                                                               |
| -------------- | -------- | ------ | --------------------------------------------------------------------------------------------------------- |
| `cost_unit_id` | Yes      | string | Cost Unit ID, copied from the Cost Unit card in North (required - updates only, no creation via the API). |
| `data`         | Yes      | array  | List of data points with date and metric values                                                           |

#### Data Point Structure <a href="#data-point-structure" id="data-point-structure"></a>

Each item in the `data` array must contain:

| Field    | Required | Type          | Description                                                                                               |
| -------- | -------- | ------------- | --------------------------------------------------------------------------------------------------------- |
| `date`   | Yes      | string (date) | YYYY-MM-DD for a daily cost unit (e.g., "2024-01-15"). YYYY-MM for a monthly cost unit (e.g., "2024-01"). |
| `metric` | Yes      | string        | Metric value as string (will be validated as decimal number)                                              |

#### Example Request <a href="#example-request.1" id="example-request.1"></a>

{% code overflow="wrap" %}

```http
POST /public/cost-units?north_account_id=123456789012
Authorization: ApiKey north_api_AbCdEfGhIjKlMnOpQrStUvWx
Content-Type: application/json

{
  "cost_unit_id": "bu-123:cu-456",
  "data": [
    { "date": "2024-01-15", "metric": "100.50" }
  ]
}
```

{% endcode %}

#### Response <a href="#response.1" id="response.1"></a>

**Success (200 OK):**

```json
{
  "status": "success",
  "data": {
    "business_unit_id": "bu-123",
    "cost_unit_id": "cu-456",
    "data_points": 2,
    "errors_count": 0,
    "warnings_count": 0,
    "errors": [],
    "warnings": [],
    "processed_data": {
      "2024-01-15": { "metric": 100.5 },
      "2024-01-16": { "metric": 150.75 }
    }
  },
  "meta": { "count": 1, "scannedCount": 1 },
  "error": null,
  "message": null
}
```

**Error Responses:**

* **400 Bad Request:** Missing Cost Unit identifiers, missing or empty data, or data that is not an array. The API also returns 400 when the business unit is not found. This happens, for example, when the list of account IDs does not match the accounts in the Cost Unit's view.
* **403 Forbidden:** The API key is missing, invalid, expired, or revoked, or does not have access to the requested resource. A 403 also means an account ID does not belong to your organization.
* **404 Not Found:** The cost unit was not found.
* **500 Internal Server Error:** An internal error occurred while processing your request

### Data Merging vs. Replacement <a href="#data-merging-vs.-replacement" id="data-merging-vs.-replacement"></a>

The POST endpoint supports two modes of operation:

* **Merge (default):** When `replace=false` or omitted, new data points are merged with existing data. If a date already exists, the new metric value will update the existing value.
* **Replace:** When `replace=true`, all existing data in the cost unit is completely replaced with the new data array. This is useful for removing dates from a cost unit by sending an array that excludes those dates.

**Tip:** Use `replace=true` when your cost unit must contain exactly the data points you send. Dates that are missing from your request are removed.

### Data Validation Notes <a href="#data-validation-notes" id="data-validation-notes"></a>

When uploading data via the POST endpoint, keep in mind:

* `metadata` and `viewId` fields are ignored if provided. Existing metadata is preserved.
* Future dates are rejected. For daily granularity, the current date is also rejected.
* Duplicate dates within a single request are rejected.
* Metric values are rounded to 2 decimal places.
* A response can be 200 OK even when some data points are rejected. Check `errors_count` in the response. Each rejected data point is listed in `errors`.

### Examples <a href="#examples" id="examples"></a>

#### Retrieve a Cost Unit <a href="#retrieve-a-cost-unit" id="retrieve-a-cost-unit"></a>

```bash
curl -X GET \
  "https://api.north.cloud/public/cost-units?north_account_id=123456789012&cost_unit_id=bu-123:cu-456" \
  -H "Authorization: ApiKey north_api_AbCdEfGhIjKlMnOpQrStUvWx"
```

#### Upload Data to a Cost Unit <a href="#upload-data-to-a-cost-unit" id="upload-data-to-a-cost-unit"></a>

```bash
curl -X POST \
  "https://api.north.cloud/public/cost-units?north_account_id=123456789012" \
  -H "Authorization: ApiKey north_api_AbCdEfGhIjKlMnOpQrStUvWx" \
  -H "Content-Type: application/json" \
  -d '{
    "cost_unit_id": "bu-123:cu-456",
    "data": [
      { "date": "2024-01-01", "metric": "1500.00" },
      { "date": "2024-01-02", "metric": "1620.50" }
    ]
  }'
```

### Recommendations <a href="#best-practices" id="best-practices"></a>

* **Secure Your API Keys:** Treat API keys like passwords. Store them securely and never commit them to version control or share them publicly.
* **Use Key Expiration:** Set expiration dates on your API keys to ensure regular rotation and limit exposure if a key is compromised.
* **Monitor Key Usage:** Regularly review your active API keys and revoke any that you do not need.
* **Validate Data Before Sending:** Ensure dates match the cost unit granularity (YYYY-MM-DD for daily, YYYY-MM for monthly) and metric values are valid decimals.
* **Handle Errors Gracefully:** Implement proper error handling in your integration to handle authentication failures, validation errors, and rate limiting.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.north.cloud/docs/features/coststreams/cost-units/api-integration.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
