---
title: Create custom allocation rule
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: Docs > API Reference > Cloud Cost Management
---

# Create custom allocation rule{% #create-custom-allocation-rule %}

{% tab title="v2" %}

| Datadog site      | API endpoint                                                  |
| ----------------- | ------------------------------------------------------------- |
| ap1.datadoghq.com | POST https://api.ap1.datadoghq.com/api/v2/cost/arbitrary_rule |
| ap2.datadoghq.com | POST https://api.ap2.datadoghq.com/api/v2/cost/arbitrary_rule |
| app.datadoghq.eu  | POST https://api.datadoghq.eu/api/v2/cost/arbitrary_rule      |
| app.ddog-gov.com  | POST https://api.ddog-gov.com/api/v2/cost/arbitrary_rule      |
| us2.ddog-gov.com  | POST https://api.us2.ddog-gov.com/api/v2/cost/arbitrary_rule  |
| app.datadoghq.com | POST https://api.datadoghq.com/api/v2/cost/arbitrary_rule     |
| us3.datadoghq.com | POST https://api.us3.datadoghq.com/api/v2/cost/arbitrary_rule |
| us5.datadoghq.com | POST https://api.us5.datadoghq.com/api/v2/cost/arbitrary_rule |

### Overview



Create a new custom allocation rule with the specified filters and allocation strategy.

**Strategy Methods:**

- **PROPORTIONAL/EVEN**: Allocates costs proportionally/evenly based on existing costs. Requires: granularity, allocated_by_tag_keys. Optional: based_on_costs, allocated_by_filters, evaluate_grouped_by_tag_keys, evaluate_grouped_by_filters.
- **PROPORTIONAL\_TIMESERIES/EVEN\_TIMESERIES**: Allocates based on timeseries data. Requires: granularity, based_on_timeseries. Optional: evaluate_grouped_by_tag_keys.
- **PERCENT**: Allocates fixed percentages to specific tags. Requires: allocated_by (array of percentage allocations).

**Filter Conditions:**

- Use **value** for single-value conditions: "is", "is not", "contains", "=", "!=", "like", "not like"
- Use **values** for multi-value conditions: "in", "not in"
- Cannot use both value and values simultaneously.

**Supported operators**: is, is not, contains, in, not in, =, !=, like, not like

OAuth apps require the `cloud_cost_management_write` authorization [scope](https://docs.datadoghq.com/api/latest/scopes.md#cloud-cost-management) to access this endpoint.



### Request

#### Body Data (required)



{% tab title="Model" %}

| Parent field                | Field                               | Type     | Description                                                                       |
| --------------------------- | ----------------------------------- | -------- | --------------------------------------------------------------------------------- |
|                             | data                                | object   | The definition of `ArbitraryCostUpsertRequestData` object.                        |
| data                        | attributes                          | object   | The definition of `ArbitraryCostUpsertRequestDataAttributes` object.              |
| attributes                  | costs_to_allocate [*required*] | [object] | The `attributes` `costs_to_allocate`.                                             |
| costs_to_allocate           | condition [*required*]         | string   | The `items` `condition`.                                                          |
| costs_to_allocate           | tag [*required*]               | string   | The `items` `tag`.                                                                |
| costs_to_allocate           | value                               | string   | The `items` `value`.                                                              |
| costs_to_allocate           | values                              | [string] | The `items` `values`.                                                             |
| attributes                  | enabled                             | boolean  | The `attributes` `enabled`.                                                       |
| attributes                  | order_id                            | int64    | The `attributes` `order_id`.                                                      |
| attributes                  | provider [*required*]          | [string] | The `attributes` `provider`.                                                      |
| attributes                  | rejected                            | boolean  | The `attributes` `rejected`.                                                      |
| attributes                  | rule_name [*required*]         | string   | The `attributes` `rule_name`.                                                     |
| attributes                  | strategy [*required*]          | object   | The definition of `ArbitraryCostUpsertRequestDataAttributesStrategy` object.      |
| strategy                    | allocated_by                        | [object] | The `strategy` `allocated_by`.                                                    |
| allocated_by                | allocated_tags [*required*]    | [object] | The `items` `allocated_tags`.                                                     |
| allocated_tags              | key [*required*]               | string   | The `items` `key`.                                                                |
| allocated_tags              | value [*required*]             | string   | The `items` `value`.                                                              |
| allocated_by                | percentage [*required*]        | double   | The `items` `percentage`. The numeric value format should be a 32bit float value. |
| strategy                    | allocated_by_filters                | [object] | The `strategy` `allocated_by_filters`.                                            |
| allocated_by_filters        | condition [*required*]         | string   | The `items` `condition`.                                                          |
| allocated_by_filters        | tag [*required*]               | string   | The `items` `tag`.                                                                |
| allocated_by_filters        | value                               | string   | The `items` `value`.                                                              |
| allocated_by_filters        | values                              | [string] | The `items` `values`.                                                             |
| strategy                    | allocated_by_tag_keys               | [string] | The `strategy` `allocated_by_tag_keys`.                                           |
| strategy                    | based_on_costs                      | [object] | The `strategy` `based_on_costs`.                                                  |
| based_on_costs              | condition [*required*]         | string   | The `items` `condition`.                                                          |
| based_on_costs              | tag [*required*]               | string   | The `items` `tag`.                                                                |
| based_on_costs              | value                               | string   | The `items` `value`.                                                              |
| based_on_costs              | values                              | [string] | The `items` `values`.                                                             |
| strategy                    | based_on_timeseries                 | object   | The `strategy` `based_on_timeseries`.                                             |
| strategy                    | evaluate_grouped_by_filters         | [object] | The `strategy` `evaluate_grouped_by_filters`.                                     |
| evaluate_grouped_by_filters | condition [*required*]         | string   | The `items` `condition`.                                                          |
| evaluate_grouped_by_filters | tag [*required*]               | string   | The `items` `tag`.                                                                |
| evaluate_grouped_by_filters | value                               | string   | The `items` `value`.                                                              |
| evaluate_grouped_by_filters | values                              | [string] | The `items` `values`.                                                             |
| strategy                    | evaluate_grouped_by_tag_keys        | [string] | The `strategy` `evaluate_grouped_by_tag_keys`.                                    |
| strategy                    | granularity                         | string   | The `strategy` `granularity`.                                                     |
| strategy                    | method [*required*]            | string   | The `strategy` `method`.                                                          |
| attributes                  | type [*required*]              | string   | The `attributes` `type`.                                                          |
| data                        | id                                  | string   | The `ArbitraryCostUpsertRequestData` `id`.                                        |
| data                        | type [*required*]              | enum     | Upsert arbitrary rule resource type. Allowed enum values: `upsert_arbitrary_rule` |

{% /tab %}

{% tab title="Example" %}

```json
{
  "data": {
    "attributes": {
      "costs_to_allocate": [
        {
          "condition": "is",
          "tag": "account_id",
          "value": "123456789"
        },
        {
          "condition": "in",
          "tag": "environment",
          "value": "",
          "values": [
            "production",
            "staging"
          ]
        }
      ],
      "enabled": true,
      "order_id": 1,
      "provider": [
        "aws",
        "gcp"
      ],
      "rule_name": "example-arbitrary-cost-rule",
      "strategy": {
        "allocated_by_tag_keys": [
          "team",
          "environment"
        ],
        "based_on_costs": [
          {
            "condition": "is",
            "tag": "service",
            "value": "web-api"
          },
          {
            "condition": "not in",
            "tag": "team",
            "value": "",
            "values": [
              "legacy",
              "deprecated"
            ]
          }
        ],
        "granularity": "daily",
        "method": "proportional"
      },
      "type": "shared"
    },
    "type": "upsert_arbitrary_rule"
  }
}
```

{% /tab %}

### Response

{% tab title="200" %}
OK
{% tab title="Model" %}
The definition of `ArbitraryRuleResponse` object.

| Parent field                | Field                                     | Type      | Description                                                                       |
| --------------------------- | ----------------------------------------- | --------- | --------------------------------------------------------------------------------- |
|                             | data                                      | object    | The definition of `ArbitraryRuleResponseData` object.                             |
| data                        | attributes                                | object    | The definition of `ArbitraryRuleResponseDataAttributes` object.                   |
| attributes                  | costs_to_allocate [*required*]       | [object]  | The `attributes` `costs_to_allocate`.                                             |
| costs_to_allocate           | condition [*required*]               | string    | The `items` `condition`.                                                          |
| costs_to_allocate           | tag [*required*]                     | string    | The `items` `tag`.                                                                |
| costs_to_allocate           | value                                     | string    | The `items` `value`.                                                              |
| costs_to_allocate           | values                                    | [string]  | The `items` `values`.                                                             |
| attributes                  | created [*required*]                 | date-time | The `attributes` `created`.                                                       |
| attributes                  | enabled [*required*]                 | boolean   | The `attributes` `enabled`.                                                       |
| attributes                  | last_modified_user_uuid [*required*] | string    | The `attributes` `last_modified_user_uuid`.                                       |
| attributes                  | order_id [*required*]                | int64     | The `attributes` `order_id`.                                                      |
| attributes                  | processing_status                         | string    | The `attributes` `processing_status`.                                             |
| attributes                  | provider [*required*]                | [string]  | The `attributes` `provider`.                                                      |
| attributes                  | rejected                                  | boolean   | The `attributes` `rejected`.                                                      |
| attributes                  | rule_name [*required*]               | string    | The `attributes` `rule_name`.                                                     |
| attributes                  | strategy [*required*]                | object    | The definition of `ArbitraryRuleResponseDataAttributesStrategy` object.           |
| strategy                    | allocated_by                              | [object]  | The `strategy` `allocated_by`.                                                    |
| allocated_by                | allocated_tags [*required*]          | [object]  | The `items` `allocated_tags`.                                                     |
| allocated_tags              | key [*required*]                     | string    | The `items` `key`.                                                                |
| allocated_tags              | value [*required*]                   | string    | The `items` `value`.                                                              |
| allocated_by                | percentage [*required*]              | double    | The `items` `percentage`. The numeric value format should be a 32bit float value. |
| strategy                    | allocated_by_filters                      | [object]  | The `strategy` `allocated_by_filters`.                                            |
| allocated_by_filters        | condition [*required*]               | string    | The `items` `condition`.                                                          |
| allocated_by_filters        | tag [*required*]                     | string    | The `items` `tag`.                                                                |
| allocated_by_filters        | value                                     | string    | The `items` `value`.                                                              |
| allocated_by_filters        | values                                    | [string]  | The `items` `values`.                                                             |
| strategy                    | allocated_by_tag_keys                     | [string]  | The `strategy` `allocated_by_tag_keys`.                                           |
| strategy                    | based_on_costs                            | [object]  | The `strategy` `based_on_costs`.                                                  |
| based_on_costs              | condition [*required*]               | string    | The `items` `condition`.                                                          |
| based_on_costs              | tag [*required*]                     | string    | The `items` `tag`.                                                                |
| based_on_costs              | value                                     | string    | The `items` `value`.                                                              |
| based_on_costs              | values                                    | [string]  | The `items` `values`.                                                             |
| strategy                    | based_on_timeseries                       | object    | The rule `strategy` `based_on_timeseries`.                                        |
| strategy                    | evaluate_grouped_by_filters               | [object]  | The `strategy` `evaluate_grouped_by_filters`.                                     |
| evaluate_grouped_by_filters | condition [*required*]               | string    | The `items` `condition`.                                                          |
| evaluate_grouped_by_filters | tag [*required*]                     | string    | The `items` `tag`.                                                                |
| evaluate_grouped_by_filters | value                                     | string    | The `items` `value`.                                                              |
| evaluate_grouped_by_filters | values                                    | [string]  | The `items` `values`.                                                             |
| strategy                    | evaluate_grouped_by_tag_keys              | [string]  | The `strategy` `evaluate_grouped_by_tag_keys`.                                    |
| strategy                    | granularity                               | string    | The `strategy` `granularity`.                                                     |
| strategy                    | method [*required*]                  | string    | The `strategy` `method`.                                                          |
| attributes                  | type [*required*]                    | string    | The `attributes` `type`.                                                          |
| attributes                  | updated [*required*]                 | date-time | The `attributes` `updated`.                                                       |
| attributes                  | version [*required*]                 | int32     | The `attributes` `version`.                                                       |
| data                        | id                                        | string    | The `ArbitraryRuleResponseData` `id`.                                             |
| data                        | type [*required*]                    | enum      | Arbitrary rule resource type. Allowed enum values: `arbitrary_rule`               |

{% /tab %}

{% tab title="Example" %}

```json
{
  "data": {
    "attributes": {
      "costs_to_allocate": [
        {
          "condition": "is",
          "tag": "account_id",
          "value": "123456789",
          "values": null
        },
        {
          "condition": "in",
          "tag": "environment",
          "value": "",
          "values": [
            "production",
            "staging"
          ]
        }
      ],
      "created": "2023-01-01T12:00:00Z",
      "enabled": true,
      "last_modified_user_uuid": "user-123-uuid",
      "order_id": 1,
      "provider": [
        "aws",
        "gcp"
      ],
      "rule_name": "Example custom allocation rule",
      "strategy": {
        "allocated_by_tag_keys": [
          "team",
          "environment"
        ],
        "based_on_costs": [
          {
            "condition": "is",
            "tag": "service",
            "value": "web-api",
            "values": null
          },
          {
            "condition": "not in",
            "tag": "team",
            "value": "",
            "values": [
              "legacy",
              "deprecated"
            ]
          }
        ],
        "granularity": "daily",
        "method": "proportional"
      },
      "type": "shared",
      "updated": "2023-01-01T12:00:00Z",
      "version": 1
    },
    "id": "123",
    "type": "arbitrary_rule"
  }
}
```

{% /tab %}

{% /tab %}

{% tab title="429" %}
Too many requests
{% tab title="Model" %}
API error response.

| Field                    | Type     | Description       |
| ------------------------ | -------- | ----------------- |
| errors [*required*] | [string] | A list of errors. |

{% /tab %}

{% tab title="Example" %}

```json
{
  "errors": [
    "Bad Request"
  ]
}
```

{% /tab %}

{% /tab %}

### Code Example

##### 
                          \## default
# 
 \# Curl command curl -X POST "https://api.datadoghq.com/api/v2/cost/arbitrary_rule" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "DD-API-KEY: ${DD_API_KEY}" \
-H "DD-APPLICATION-KEY: ${DD_APP_KEY}" \
-d @- << EOF
{
  "data": {
    "attributes": {
      "costs_to_allocate": [
        {
          "condition": "is",
          "tag": "account_id",
          "value": "123456789"
        },
        {
          "condition": "in",
          "tag": "environment",
          "value": "",
          "values": [
            "production",
            "staging"
          ]
        }
      ],
      "enabled": true,
      "order_id": 1,
      "provider": [
        "aws",
        "gcp"
      ],
      "rule_name": "example-arbitrary-cost-rule",
      "strategy": {
        "allocated_by_tag_keys": [
          "team",
          "environment"
        ],
        "based_on_costs": [
          {
            "condition": "is",
            "tag": "service",
            "value": "web-api"
          },
          {
            "condition": "not in",
            "tag": "team",
            "value": "",
            "values": [
              "legacy",
              "deprecated"
            ]
          }
        ],
        "granularity": "daily",
        "method": "proportional"
      },
      "type": "shared"
    },
    "type": "upsert_arbitrary_rule"
  }
}
EOF 
                        
##### 

```go
// Create custom allocation rule returns "OK" response

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"os"

	"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
	"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
)

func main() {
	body := datadogV2.ArbitraryCostUpsertRequest{
		Data: &datadogV2.ArbitraryCostUpsertRequestData{
			Attributes: &datadogV2.ArbitraryCostUpsertRequestDataAttributes{
				CostsToAllocate: []datadogV2.ArbitraryCostUpsertRequestDataAttributesCostsToAllocateItems{
					{
						Condition: "is",
						Tag:       "account_id",
						Value:     datadog.PtrString("123456789"),
					},
					{
						Condition: "in",
						Tag:       "environment",
						Value:     datadog.PtrString(""),
						Values: *datadog.NewNullableList(&[]string{
							"production",
							"staging",
						}),
					},
				},
				Enabled: datadog.PtrBool(true),
				OrderId: datadog.PtrInt64(1),
				Provider: []string{
					"aws",
					"gcp",
				},
				RuleName: "example-arbitrary-cost-rule",
				Strategy: datadogV2.ArbitraryCostUpsertRequestDataAttributesStrategy{
					AllocatedByTagKeys: []string{
						"team",
						"environment",
					},
					BasedOnCosts: []datadogV2.ArbitraryCostUpsertRequestDataAttributesStrategyBasedOnCostsItems{
						{
							Condition: "is",
							Tag:       "service",
							Value:     datadog.PtrString("web-api"),
						},
						{
							Condition: "not in",
							Tag:       "team",
							Value:     datadog.PtrString(""),
							Values: *datadog.NewNullableList(&[]string{
								"legacy",
								"deprecated",
							}),
						},
					},
					Granularity: datadog.PtrString("daily"),
					Method:      "proportional",
				},
				Type: "shared",
			},
			Type: datadogV2.ARBITRARYCOSTUPSERTREQUESTDATATYPE_UPSERT_ARBITRARY_RULE,
		},
	}
	ctx := datadog.NewDefaultContext(context.Background())
	configuration := datadog.NewConfiguration()
	apiClient := datadog.NewAPIClient(configuration)
	api := datadogV2.NewCloudCostManagementApi(apiClient)
	resp, r, err := api.CreateCustomAllocationRule(ctx, body)

	if err != nil {
		fmt.Fprintf(os.Stderr, "Error when calling `CloudCostManagementApi.CreateCustomAllocationRule`: %v\n", err)
		fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
	}

	responseContent, _ := json.MarshalIndent(resp, "", "  ")
	fmt.Fprintf(os.Stdout, "Response from `CloudCostManagementApi.CreateCustomAllocationRule`:\n%s\n", responseContent)
}
```

#### Instructions

First [install the library and its dependencies](https://docs.datadoghq.com/api/latest.md?code-lang=go) and then save the example to `main.go` and run following commands:
    DD_SITE="datadoghq.com" DD_API_KEY="<DD_API_KEY>" DD_APP_KEY="<DD_APP_KEY>" go run "main.go"
##### 

```java
// Create custom allocation rule returns "OK" response

import com.datadog.api.client.ApiClient;
import com.datadog.api.client.ApiException;
import com.datadog.api.client.v2.api.CloudCostManagementApi;
import com.datadog.api.client.v2.model.ArbitraryCostUpsertRequest;
import com.datadog.api.client.v2.model.ArbitraryCostUpsertRequestData;
import com.datadog.api.client.v2.model.ArbitraryCostUpsertRequestDataAttributes;
import com.datadog.api.client.v2.model.ArbitraryCostUpsertRequestDataAttributesCostsToAllocateItems;
import com.datadog.api.client.v2.model.ArbitraryCostUpsertRequestDataAttributesStrategy;
import com.datadog.api.client.v2.model.ArbitraryCostUpsertRequestDataAttributesStrategyBasedOnCostsItems;
import com.datadog.api.client.v2.model.ArbitraryCostUpsertRequestDataType;
import com.datadog.api.client.v2.model.ArbitraryRuleResponse;
import java.util.Arrays;

public class Example {
  public static void main(String[] args) {
    ApiClient defaultClient = ApiClient.getDefaultApiClient();
    CloudCostManagementApi apiInstance = new CloudCostManagementApi(defaultClient);

    ArbitraryCostUpsertRequest body =
        new ArbitraryCostUpsertRequest()
            .data(
                new ArbitraryCostUpsertRequestData()
                    .attributes(
                        new ArbitraryCostUpsertRequestDataAttributes()
                            .costsToAllocate(
                                Arrays.asList(
                                    new ArbitraryCostUpsertRequestDataAttributesCostsToAllocateItems()
                                        .condition("is")
                                        .tag("account_id")
                                        .value("123456789"),
                                    new ArbitraryCostUpsertRequestDataAttributesCostsToAllocateItems()
                                        .condition("in")
                                        .tag("environment")
                                        .value("")
                                        .values(Arrays.asList("production", "staging"))))
                            .enabled(true)
                            .orderId(1L)
                            .provider(Arrays.asList("aws", "gcp"))
                            .ruleName("example-arbitrary-cost-rule")
                            .strategy(
                                new ArbitraryCostUpsertRequestDataAttributesStrategy()
                                    .allocatedByTagKeys(Arrays.asList("team", "environment"))
                                    .basedOnCosts(
                                        Arrays.asList(
                                            new ArbitraryCostUpsertRequestDataAttributesStrategyBasedOnCostsItems()
                                                .condition("is")
                                                .tag("service")
                                                .value("web-api"),
                                            new ArbitraryCostUpsertRequestDataAttributesStrategyBasedOnCostsItems()
                                                .condition("not in")
                                                .tag("team")
                                                .value("")
                                                .values(Arrays.asList("legacy", "deprecated"))))
                                    .granularity("daily")
                                    .method("proportional"))
                            .type("shared"))
                    .type(ArbitraryCostUpsertRequestDataType.UPSERT_ARBITRARY_RULE));

    try {
      ArbitraryRuleResponse result = apiInstance.createCustomAllocationRule(body);
      System.out.println(result);
    } catch (ApiException e) {
      System.err.println(
          "Exception when calling CloudCostManagementApi#createCustomAllocationRule");
      System.err.println("Status code: " + e.getCode());
      System.err.println("Reason: " + e.getResponseBody());
      System.err.println("Response headers: " + e.getResponseHeaders());
      e.printStackTrace();
    }
  }
}
```

#### Instructions

First [install the library and its dependencies](https://docs.datadoghq.com/api/latest.md?code-lang=java) and then save the example to `Example.java` and run following commands:
    DD_SITE="datadoghq.com" DD_API_KEY="<DD_API_KEY>" DD_APP_KEY="<DD_APP_KEY>" java "Example.java"
##### 

```python
"""
Create custom allocation rule returns "OK" response
"""

from datadog_api_client import ApiClient, Configuration
from datadog_api_client.v2.api.cloud_cost_management_api import CloudCostManagementApi
from datadog_api_client.v2.model.arbitrary_cost_upsert_request import ArbitraryCostUpsertRequest
from datadog_api_client.v2.model.arbitrary_cost_upsert_request_data import ArbitraryCostUpsertRequestData
from datadog_api_client.v2.model.arbitrary_cost_upsert_request_data_attributes import (
    ArbitraryCostUpsertRequestDataAttributes,
)
from datadog_api_client.v2.model.arbitrary_cost_upsert_request_data_attributes_costs_to_allocate_items import (
    ArbitraryCostUpsertRequestDataAttributesCostsToAllocateItems,
)
from datadog_api_client.v2.model.arbitrary_cost_upsert_request_data_attributes_strategy import (
    ArbitraryCostUpsertRequestDataAttributesStrategy,
)
from datadog_api_client.v2.model.arbitrary_cost_upsert_request_data_attributes_strategy_based_on_costs_items import (
    ArbitraryCostUpsertRequestDataAttributesStrategyBasedOnCostsItems,
)
from datadog_api_client.v2.model.arbitrary_cost_upsert_request_data_type import ArbitraryCostUpsertRequestDataType

body = ArbitraryCostUpsertRequest(
    data=ArbitraryCostUpsertRequestData(
        attributes=ArbitraryCostUpsertRequestDataAttributes(
            costs_to_allocate=[
                ArbitraryCostUpsertRequestDataAttributesCostsToAllocateItems(
                    condition="is",
                    tag="account_id",
                    value="123456789",
                ),
                ArbitraryCostUpsertRequestDataAttributesCostsToAllocateItems(
                    condition="in",
                    tag="environment",
                    value="",
                    values=[
                        "production",
                        "staging",
                    ],
                ),
            ],
            enabled=True,
            order_id=1,
            provider=[
                "aws",
                "gcp",
            ],
            rule_name="example-arbitrary-cost-rule",
            strategy=ArbitraryCostUpsertRequestDataAttributesStrategy(
                allocated_by_tag_keys=[
                    "team",
                    "environment",
                ],
                based_on_costs=[
                    ArbitraryCostUpsertRequestDataAttributesStrategyBasedOnCostsItems(
                        condition="is",
                        tag="service",
                        value="web-api",
                    ),
                    ArbitraryCostUpsertRequestDataAttributesStrategyBasedOnCostsItems(
                        condition="not in",
                        tag="team",
                        value="",
                        values=[
                            "legacy",
                            "deprecated",
                        ],
                    ),
                ],
                granularity="daily",
                method="proportional",
            ),
            type="shared",
        ),
        type=ArbitraryCostUpsertRequestDataType.UPSERT_ARBITRARY_RULE,
    ),
)

configuration = Configuration()
with ApiClient(configuration) as api_client:
    api_instance = CloudCostManagementApi(api_client)
    response = api_instance.create_custom_allocation_rule(body=body)

    print(response)
```

#### Instructions

First [install the library and its dependencies](https://docs.datadoghq.com/api/latest.md?code-lang=python) and then save the example to `example.py` and run following commands:
    DD_SITE="datadoghq.com" DD_API_KEY="<DD_API_KEY>" DD_APP_KEY="<DD_APP_KEY>" python3 "example.py"
##### 

```ruby
# Create custom allocation rule returns "OK" response

require "datadog_api_client"
api_instance = DatadogAPIClient::V2::CloudCostManagementAPI.new

body = DatadogAPIClient::V2::ArbitraryCostUpsertRequest.new({
  data: DatadogAPIClient::V2::ArbitraryCostUpsertRequestData.new({
    attributes: DatadogAPIClient::V2::ArbitraryCostUpsertRequestDataAttributes.new({
      costs_to_allocate: [
        DatadogAPIClient::V2::ArbitraryCostUpsertRequestDataAttributesCostsToAllocateItems.new({
          condition: "is",
          tag: "account_id",
          value: "123456789",
        }),
        DatadogAPIClient::V2::ArbitraryCostUpsertRequestDataAttributesCostsToAllocateItems.new({
          condition: "in",
          tag: "environment",
          value: "",
          values: [
            "production",
            "staging",
          ],
        }),
      ],
      enabled: true,
      order_id: 1,
      provider: [
        "aws",
        "gcp",
      ],
      rule_name: "example-arbitrary-cost-rule",
      strategy: DatadogAPIClient::V2::ArbitraryCostUpsertRequestDataAttributesStrategy.new({
        allocated_by_tag_keys: [
          "team",
          "environment",
        ],
        based_on_costs: [
          DatadogAPIClient::V2::ArbitraryCostUpsertRequestDataAttributesStrategyBasedOnCostsItems.new({
            condition: "is",
            tag: "service",
            value: "web-api",
          }),
          DatadogAPIClient::V2::ArbitraryCostUpsertRequestDataAttributesStrategyBasedOnCostsItems.new({
            condition: "not in",
            tag: "team",
            value: "",
            values: [
              "legacy",
              "deprecated",
            ],
          }),
        ],
        granularity: "daily",
        method: "proportional",
      }),
      type: "shared",
    }),
    type: DatadogAPIClient::V2::ArbitraryCostUpsertRequestDataType::UPSERT_ARBITRARY_RULE,
  }),
})
p api_instance.create_custom_allocation_rule(body)
```

#### Instructions

First [install the library and its dependencies](https://docs.datadoghq.com/api/latest.md?code-lang=ruby) and then save the example to `example.rb` and run following commands:
    DD_SITE="datadoghq.com" DD_API_KEY="<DD_API_KEY>" DD_APP_KEY="<DD_APP_KEY>" rb "example.rb"
##### 

```rust
// Create custom allocation rule returns "OK" response
use datadog_api_client::datadog;
use datadog_api_client::datadogV2::api_cloud_cost_management::CloudCostManagementAPI;
use datadog_api_client::datadogV2::model::ArbitraryCostUpsertRequest;
use datadog_api_client::datadogV2::model::ArbitraryCostUpsertRequestData;
use datadog_api_client::datadogV2::model::ArbitraryCostUpsertRequestDataAttributes;
use datadog_api_client::datadogV2::model::ArbitraryCostUpsertRequestDataAttributesCostsToAllocateItems;
use datadog_api_client::datadogV2::model::ArbitraryCostUpsertRequestDataAttributesStrategy;
use datadog_api_client::datadogV2::model::ArbitraryCostUpsertRequestDataAttributesStrategyBasedOnCostsItems;
use datadog_api_client::datadogV2::model::ArbitraryCostUpsertRequestDataType;

#[tokio::main]
async fn main() {
    let body = ArbitraryCostUpsertRequest::new().data(
        ArbitraryCostUpsertRequestData::new(
            ArbitraryCostUpsertRequestDataType::UPSERT_ARBITRARY_RULE,
        )
        .attributes(
            ArbitraryCostUpsertRequestDataAttributes::new(
                vec![
                    ArbitraryCostUpsertRequestDataAttributesCostsToAllocateItems::new(
                        "is".to_string(),
                        "account_id".to_string(),
                    )
                    .value("123456789".to_string()),
                    ArbitraryCostUpsertRequestDataAttributesCostsToAllocateItems::new(
                        "in".to_string(),
                        "environment".to_string(),
                    )
                    .value("".to_string())
                    .values(Some(vec!["production".to_string(), "staging".to_string()])),
                ],
                vec!["aws".to_string(), "gcp".to_string()],
                "example-arbitrary-cost-rule".to_string(),
                ArbitraryCostUpsertRequestDataAttributesStrategy::new("proportional".to_string())
                    .allocated_by_tag_keys(vec!["team".to_string(), "environment".to_string()])
                    .based_on_costs(vec![
                        ArbitraryCostUpsertRequestDataAttributesStrategyBasedOnCostsItems::new(
                            "is".to_string(),
                            "service".to_string(),
                        )
                        .value("web-api".to_string()),
                        ArbitraryCostUpsertRequestDataAttributesStrategyBasedOnCostsItems::new(
                            "not in".to_string(),
                            "team".to_string(),
                        )
                        .value("".to_string())
                        .values(Some(vec!["legacy".to_string(), "deprecated".to_string()])),
                    ])
                    .granularity("daily".to_string()),
                "shared".to_string(),
            )
            .enabled(true)
            .order_id(1),
        ),
    );
    let configuration = datadog::Configuration::new();
    let api = CloudCostManagementAPI::with_config(configuration);
    let resp = api.create_custom_allocation_rule(body).await;
    if let Ok(value) = resp {
        println!("{:#?}", value);
    } else {
        println!("{:#?}", resp.unwrap_err());
    }
}
```

#### Instructions

First [install the library and its dependencies](https://docs.datadoghq.com/api/latest.md?code-lang=rust) and then save the example to `src/main.rs` and run following commands:
    DD_SITE="datadoghq.com" DD_API_KEY="<DD_API_KEY>" DD_APP_KEY="<DD_APP_KEY>" cargo run
##### 

```typescript
/**
 * Create custom allocation rule returns "OK" response
 */

import { client, v2 } from "@datadog/datadog-api-client";

const configuration = client.createConfiguration();
const apiInstance = new v2.CloudCostManagementApi(configuration);

const params: v2.CloudCostManagementApiCreateCustomAllocationRuleRequest = {
  body: {
    data: {
      attributes: {
        costsToAllocate: [
          {
            condition: "is",
            tag: "account_id",
            value: "123456789",
          },
          {
            condition: "in",
            tag: "environment",
            value: "",
            values: ["production", "staging"],
          },
        ],
        enabled: true,
        orderId: 1,
        provider: ["aws", "gcp"],
        ruleName: "example-arbitrary-cost-rule",
        strategy: {
          allocatedByTagKeys: ["team", "environment"],
          basedOnCosts: [
            {
              condition: "is",
              tag: "service",
              value: "web-api",
            },
            {
              condition: "not in",
              tag: "team",
              value: "",
              values: ["legacy", "deprecated"],
            },
          ],
          granularity: "daily",
          method: "proportional",
        },
        type: "shared",
      },
      type: "upsert_arbitrary_rule",
    },
  },
};

apiInstance
  .createCustomAllocationRule(params)
  .then((data: v2.ArbitraryRuleResponse) => {
    console.log(
      "API called successfully. Returned data: " + JSON.stringify(data)
    );
  })
  .catch((error: any) => console.error(error));
```

#### Instructions

First [install the library and its dependencies](https://docs.datadoghq.com/api/latest.md?code-lang=typescript) and then save the example to `example.ts` and run following commands:
    DD_SITE="datadoghq.com" DD_API_KEY="<DD_API_KEY>" DD_APP_KEY="<DD_APP_KEY>" tsc "example.ts"
{% /tab %}
