Skip to main content

Plan JSON API

The majority of users should be able to use Infracost CI/CD integrations. However, when a self-service provisioning portal such ServiceNow or Atlantis needs to integrate with Infracost, an HTTP library is often the simplest option. This API accepts a Terraform plan JSON file and returns the results in various formats.

To use this API, send an HTTP POST request to desired endpoint (e.g. https://pricing.api.infracost.io/diff) with a Terraform plan JSON file sent in the path parameter using the multipart/form-data request body format, as shown in the following example curl request. The x-api-key header must be set to your Infracost API key.

This API deletes files from the server after they are processed, however, it is also good security practice to remove secrets from the file before sending it to the API. For example, AWS provides a grep command that can be used to do this.

Breakdown

Show breakdown of costs. This is the main API call that should be used. It can return a JSON response, which contains the total costs as well as detailed breakdowns:

cat infracost.json | jq -r '.totalMonthlyCost'
cat infracost.json | jq -r '.pastTotalMonthlyCost'
cat infracost.json | jq -r '.diffTotalMonthlyCost'

Send an HTTP POST to: https://pricing.api.infracost.io/breakdown

HeaderDescriptionNotes
x-api-keyInfracost API KeyRequired. Must be set to your Infracost API key.
ParameterDescriptionNotes
pathTerraform plan JSON fileRequired. Use '@' to upload the file with curl, e.g. -F "path=@plan.json"
usage-fileInfracost usage file that specifies values for usage-based resourcesNot required. Use '@' to upload the file with curl, e.g. -F "usage-file=@infracost-usage.yml"
show-skippedList unsupported and free resources.Not required. Defaults to false
no-colorTurn off colored output, useful for CI/CD usersNot required. Defaults to false
formatContent type of the responseNot required. Must be one of table, html or json. Defaults to table
fieldsFields to include in the responseNot required, supported by table and html formats. Must be a comma separated list of fields: all, price, monthlyQuantity, unit, hourlyCost, monthlyCost. Defaults to monthlyQuantity,unit,monthlyCost

Examples

cd path/to/code
terraform init
terraform plan -out tfplan.binary
terraform show -json tfplan.binary > plan.json

curl -X POST -H "x-api-key: my-api-key" -F "ci-platform=atlantis" \
-F "path=@plan.json" \
https://pricing.api.infracost.io/breakdown

Diff

The breakdown API call should be used most of the time as it supports a JSON format and includes the breakdown as well as the diff of the costs (between current and planned state).

This API call is useful when you want to get a text diff format, similar to git diff, as shown in the example response below.

Send an HTTP POST to: https://pricing.api.infracost.io/diff

HeaderDescriptionNotes
x-api-keyInfracost API KeyRequired. Must be set to your Infracost API key.
ParameterDescriptionNotes
pathTerraform plan JSON fileRequired. Use '@' to upload the file with curl, e.g. -F "path=@plan.json"
usage-fileInfracost usage file that specifies values for usage-based resourcesNot required. Use '@' to upload the file with curl, e.g. -F "usage-file=@infracost-usage.yml"
show-skippedList unsupported and free resources.Not required. Defaults to false
no-colorTurn off colored output, useful for CI/CD usersNot required. Defaults to false

Examples

cd path/to/code
terraform init
terraform plan -out tfplan.binary
terraform show -json tfplan.binary > plan.json

curl -X POST -H "x-api-key: my-api-key" -F "ci-platform=atlantis" \
-F "path=@plan.json" \
https://pricing.api.infracost.io/diff