Skip to main content

API Examples

note

Throughout our API documentation, we use localhost:9003 as the default OpenCost API URL, but your OpenCost instance may be exposed by a service or ingress. To reach the OpenCost API at port 9003, run: kubectl -n opencost port-forward deployment/opencost 9003. You may also expose the OpenCost UI on port 9090 with the command kubectl -n opencost port-forward deployment/opencost 9003 9090.

/allocation Examples

The OpenCost /allocation API has query parameters for window, aggregate, step, and resolution. Below are several examples of queries using this API. Please refer to the OpenCost API for the full API specifics.

Default OpenCost UI Allocation request

Here is an example of an OpenCost API query for the last 7 days of namespaces every minute:

curl -G http://localhost:9003/allocation \
-d window=7d \
-d aggregate=namespace \
-d resolution=1m

This is the default query for the OpenCost UI and produces output similar to this.

Allocation data for the last 60m, in steps of 10m, with resolution 1m, aggregated by namespace

curl -G http://localhost:9003/allocation \
-d window=60m \
-d step=10m \
-d resolution=1m \
-d aggregate=namespace
{
"code": 200,
"data": [
{
"kube-system": { ... },
"default": { ... },
"cost-model": { ... }
}
]
}

Allocation data for the last 9d, in steps of 3d, with a 10m resolution, aggregated by namespace

curl -G http://localhost:9003/allocation/compute \
-d window=9d \
-d step=3d \
-d resolution=10m
-d aggregate=namespace
{
"code": 200,
"data": [
{
"default": { ... },
"open-cost": { ... },
"kube-system": { ... }
},
{
"default": { ... },
"open-cost": { ... },
"kube-system": { ... }
},
{
"default": { ... },
"open-cost": { ... },
"kube-system": { ... }
}
]
}

/cloudCost Examples

The OpenCost /cloudCost API has query parameters for window, aggregate, and filter. Below are several examples of queries using this API. Please refer to the OpenCost API for the full API specifics.

Default OpenCost UI Cloud Costs request

Amortized Net Cost per Provider for the last 7 days

curl -G http://localhost:9003/cloudCost \
-d window=7d \
-d aggregate=provider

This is the default query for the OpenCost UI Cloud Costs and produces output similar to this.

List Cost per Provider and Service for the past 14 days

This example shows list cost multi-aggregation by Provider and Service.

curl -G http://localhost:9003/cloudCost \
-d window=14d \
-d aggregate=provider,service
{
"code": 200,
"data": {
"sets": [
{
"cloudCosts": {
"AWS/AWSBackup": { ... },
"AWS/AWSCloudTrail": {
"properties": {
"provider": "AWS",
"invoiceEntityID": "297945954695",
"service": "AWSCloudTrail",
"category": "Other"
},
"window": {
"start": "2023-10-28T00:00:00Z",
"end": "2023-10-29T00:00:00Z"
},
"listCost": {
"cost": 19.800393,
"kubernetesPercent": 0
},
"netCost": {
"cost": 19.800393,
"kubernetesPercent": 0
},
"amortizedNetCost": {
"cost": 19.800393,
"kubernetesPercent": 0
},
"invoicedCost": {
"cost": 19.800393,
"kubernetesPercent": 0
},
"amortizedCost": {
"cost": 19.800393,
"kubernetesPercent": 0
}
},
"AWS/AWSCostExplorer": { ... },
"AWS/AWSELB": { ... },
"AWS/AWSGlobalAccelerator": { ... },
...
"GCP/Secret Manager": { ... }
},
"aggregationProperties": [
"provider",
"service"
]
}
],
"window": {
"start": "null",
"end": "null"
}
}
}

All billing items for a period of 3 days

A warning, this may return many megabytes of data depending on the cloud provider usage. Example output was heavily pared down from 17 megabytes.

curl -G http://localhost:9003/cloudCost \
-d window=2023-10-28T00:00:00Z,2023-10-31T00:00:00Z
{
"code": 200,
"data": {
"sets": [
{
"cloudCosts": {
...
"HDEAD-C6F51B-690123/mattray-opencost/GCP/__unallocated__/Management/Kubernetes Engine": {
"properties": {
"provider": "GCP",
"accountID": "mattray-opencost",
"invoiceEntityID": "HDEAD-C6F51B-690123",
"service": "Kubernetes Engine",
"category": "Management",
"labels": {
"goog-fleet-project": "",
"goog-k8s-cluster-location": "australia-southeast1-b",
"goog-k8s-cluster-name": "opencost-3",
"longlived": "opencost"
}
},
"window": {
"start": "2023-10-28T00:00:00Z",
"end": "2023-10-29T00:00:00Z"
},
"listCost": {
"cost": 2.399978,
"kubernetesPercent": 1
},
"netCost": {
"cost": 2.399978,
"kubernetesPercent": 1
},
"amortizedNetCost": {
"cost": 2.399978,
"kubernetesPercent": 1
},
"invoicedCost": {
"cost": 2.399978,
"kubernetesPercent": 1
},
"amortizedCost": {
"cost": 2.399978,
"kubernetesPercent": 1
}
},
"HDEAD-C6F51B-690123/mattray-opencost/GCP/__unallocated__/Network/Networking": {
"properties": {
"provider": "GCP",
"accountID": "mattray-opencost",
"invoiceEntityID": "HDEAD-C6F51B-690123",
"service": "Networking",
"category": "Network"
},
"window": {
"start": "2023-10-28T00:00:00Z",
"end": "2023-10-29T00:00:00Z"
},
"listCost": {
"cost": 0.46231399999999995,
"kubernetesPercent": 0
},
"netCost": {
"cost": 0.006336000000000003,
"kubernetesPercent": 0
},
"amortizedNetCost": {
"cost": 0.006336000000000003,
"kubernetesPercent": 0
},
"invoicedCost": {
"cost": 0.006336000000000003,
"kubernetesPercent": 0
},
"amortizedCost": {
"cost": 0.006336000000000003,
"kubernetesPercent": 0
}
},
...
"HDEAD-C6F51B-690123/mattray-opencost/GCP/gke-opencost-3-default-pool-123456f4-kbl5/Other/Compute Engine": {
"properties": {
"providerID": "gke-opencost-3-default-pool-123456f4-kbl5",
"provider": "GCP",
"accountID": "mattray-opencost",
"invoiceEntityID": "HDEAD-C6F51B-690123",
"service": "Compute Engine",
"category": "Other",
"labels": {
"goog-gke-node": "",
"goog-k8s-cluster-location": "australia-southeast1-b",
"goog-k8s-cluster-name": "opencost-3",
"goog-k8s-node-pool-name": "default-pool",
"longlived": "opencost"
}
},
"window": {
"start": "2023-10-28T00:00:00Z",
"end": "2023-10-29T00:00:00Z"
},
"listCost": {
"cost": 0,
"kubernetesPercent": 0
},
"netCost": {
"cost": 0,
"kubernetesPercent": 0
},
"amortizedNetCost": {
"cost": 0,
"kubernetesPercent": 0
},
"invoicedCost": {
"cost": 0,
"kubernetesPercent": 0
},
"amortizedCost": {
"cost": 0,
"kubernetesPercent": 0
}
},
...
"window": {
"start": "2023-10-30T00:00:00Z",
"end": "2023-10-31T00:00:00Z"
},
"aggregationProperties": [
"invoiceEntityID",
"accountID",
"provider",
"providerID",
"category",
"service"
]
}
],
"window": {
"start": "null",
"end": "null"
}
}
}

/customCost Datadog External Costs Examples

Please refer to the OpenCost API for the full API specifics.

Totals request, aggregating by domain over the past 7 days:

https://example.opencost.io/model/customCost/total?window=7d&aggregate=domain

Response:

{
"code": 200,
"data": {
"window": {
"start": "2024-03-14T00:00:00Z",
"end": "2024-03-21T00:00:00Z"
},
"totalBilledCost": 147.37999,
"totalListCost": 186.98547,
"customCosts": [
{
"id": "",
"zone": "us",
"account_name": "Kubecost",
"charge_category": "",
"description": "",
"resource_name": "",
"resource_type": "",
"provider_id": "",
"billedCost": 147.37999,
"listCost": 186.98547,
"list_unit_price": 0.082197905,
"usage_quantity": 120,
"usage_unit": "",
"domain": "datadog",
"cost_source": "observability",
"aggregate": "datadog"
}
]
}
}

Totals request, aggregating by providerId over the past 7 days:

https://example.opencost.io/model/customCost/total?window=7d&aggregate=providerId

Response:

{
"code": 200,
"data": {
"window": {
"start": "2024-03-14T00:00:00Z",
"end": "2024-03-21T00:00:00Z"
},
"totalBilledCost": 147.37999,
"totalListCost": 186.98546,
"customCosts": [
{
"id": "",
"zone": "us",
"account_name": "Kubecost",
"charge_category": "usage",
"description": "350+ integrations, alerting, custom metrics & unlimited user accounts",
"resource_name": "agent_host_count",
"resource_type": "infra_hosts",
"provider_id": "42c0ac62-8d80-11ed-96f3-da7ad0900005/agent_host_count",
"billedCost": 0,
"listCost": 8.876712,
"list_unit_price": 0.073972605,
"usage_quantity": 360,
"usage_unit": "Infra Host - hours",
"domain": "datadog",
"cost_source": "observability",
"aggregate": "42c0ac62-8d80-11ed-96f3-da7ad0900005/agent_host_count"
},
{
"id": "",
"zone": "us",
"account_name": "Kubecost",
"charge_category": "usage",
"description": "Centralize your monitoring of systems and services (Per Container)",
"resource_name": "container_count_excl_agent",
"resource_type": "infra_hosts",
"provider_id": "42c0ac62-8d80-11ed-96f3-da7ad0900005/container_count_excl_agent",
"billedCost": 0,
"listCost": 19.80274,
"list_unit_price": 0.0041095894,
"usage_quantity": 14456,
"usage_unit": "Container - hours",
"domain": "datadog",
"cost_source": "observability",
"aggregate": "42c0ac62-8d80-11ed-96f3-da7ad0900005/container_count_excl_agent"
}
...
]
}
}

Postman

A collection of OpenCost Postman queries: opencost.postman_collection.json

Note: Change the hostname in the Collection>Edit>Variables

Documentation Distributed under CC BY 4.0.  The Linux Foundation® (TLF) has registered trademarks and uses trademarks. For a list of TLF trademarks, see: Trademark Usage.