Querying consumption metrics with the API
Learn how to get a variety of consumption metrics using the Neon API
Using the Neon API, you can query a range of account-level and project-level metrics to help gauge your resource consumption.
To learn more about which metrics you can get reports on, see Available metrics on the Manage billing with consumption limits page.
Here are the different ways to retrieve these metrics, depending on how you want them aggregated or broken down:
Endpoint | Description | Plan Availability |
---|---|---|
Account-level cumulative metrics | Aggregates all metrics from all projects in an account into a single cumulative number for each metric | Scale plan only |
Granular project-level metrics | Provides detailed metrics for each project in an account at a specified granularity level (e.g., hourly, daily, monthly) | Scale plan only |
Billing period project-level metrics | Offers consumption metrics for each project in an account for the current billing period | All plans |
Single project metrics | Retrieves detailed metrics and quota information for a specific project | All plans |
Get account-level aggregated metrics
Using the Get account consumption metrics API, you can find total usage across all projects in your organization. This provides a comprehensive view of consumption metrics accumulated for the billing period.
Here is the URL in the Neon API where you can get account-level metrics:
This API endpoint accepts the following query parameters: from
, to
, granularity
, org_id
, and include_v1_metrics
.
Choosing your account
Include the unique org_id
for your organization to retrieve account metrics for that specific organization. If not specified, metrics for your personal account will be returned.
note
Organizations are currently in private preview. For more information about this upcoming feature, see Organizations.
Set a date range for granular results
You can set from
and to
query parameters, plus a level of granularity to define a time range that can span across multiple billing periods.
from
— Sets the start date and time of the time period for which you are seeking metrics.to
— Sets the end date and time for the interval for which you desire metrics.granularity
— Sets the level of granularity for the metrics, such ashourly
,daily
, ormonthly
.
The response is organized by periods and consumption data within the specified time range.
See Details on setting a date range for more info.
Get granular project-level metrics for your account
You can also get similar daily, hourly, or monthly metrics across a selected time period, but broken out for each individual project that belongs to your organization.
Using the endpoint GET /consumption_history/projects
, let's use the same start date, end date, and level of granularity as our account-level request: hourly metrics between June 30th and July 2nd, 2024.
Response body
The response is organized by periods and consumption data within the specified time range.
See Details on setting a date range for more info.
Pagination
To control pagination (number of results per response), you can include these query parameters:
limit
— sets the number of project objects to be included in the response.cursor
— by default, the response uses the projectid
from the last project in the list as thecursor
value (included in thepagination
object at the end of the response). Generally, it is up to the application to collect and use this cursor value when setting up the next request.
See Details on pagination for more info.
Get project-level metrics for your account by billing period
Use the Consumption API to get a full list of key consumption metrics for all the projects in your Neon account in one request. You can specify a date range to get metrics from across multiple billing periods and control pagination for large result sets.
Here is the URL in the Neon API where you can get details for all projects in your account:
This API endpoint accepts the following query parameters: from
, to
, limit
, and cursor
.
Set a date range across multiple billing periods
You can set from
and to
query parameters to define a time range that can span across multiple billing periods.
from
— Sets the start date and time of the time period for which you are seeking metrics.to
— Sets the end date and time for the interval for which you desire metrics.
The response is organized by project and billing period: one object per project, per active billing period within the range. For example, if you choose a 6-month time range you will get up to 6 objects for every project active within those months. The response includes any projects deleted within that time range.
If you do not include these parameters, the query defaults to the current consumption period.
Here is an example query that returns metrics from September 1st and December 1st, 2023. Time values must be provided in ISO 8601 format. You can use this timestamp converter.
And here is a sample response (with key lines highlighted):
Response body
Key details:
- The
period_id
key andprevious_period_id
are unique values used to identify and connect periods across the time range. - The
period_start
andperiod_end
keys show the dates for that particular billing period. Anull
value indicates that the object is for the current billing period. - The
cursor
object underpagination
shows the last project Id in the response. See Details on pagination for more.
Get metrics for a single specified project
Using a GET
request from the Neon API (see Get project details), you can find the following consumption details for a given project:
- Current consumption metrics accumulated for the billing period
- Start and end dates for the billing period
- Current usage quotas (max limits) configured for the project
Using these details, you can set up the logic for when to send notification warnings, when to reset a quota, and other possible actions related to the pending or current suspension of a project's active computes.
Here is an example a GET
request for an individual project.
And here is what the response might look like.
Response body
Looking at this response, here are some conclusions we can draw:
-
This project is 1 hour away from being suspended.
With current
compute_time_seconds
at 68,400 (19 hours) and the quota for that metric set at 72,000 (20 hours), the project is only 1 hour of compute time away from being suspended. -
This project is 1 day away from a quota refresh.
If today's date is October 31st, 2023, and the
consumption_period_end
parameter is 2023-11-01T00:00:00Z (November 1st, 2023), then the project has 1 day left before all quota parameters (except forlogical_byte_size
) are refreshed.
Details on setting a date range
This section applies to the following metrics output types: Account-level aggregated metrics, and Granular project-level metrics for your account.
You can set from
and to
query parameters, plus a level of granularity to define a time range that can span across multiple billing periods.
from
— Sets the start date and time of the time period for which you are seeking metrics.to
— Sets the end date and time for the interval for which you desire metrics.granularity
— Sets the level of granularity for the metrics, such ashourly
,daily
, ormonthly
.
The response is organized by periods and consumption data within the specified time range.
Here is an example query that returns metrics from June 30th to July 2nd, 2024. Time values must be provided in ISO 8601 format. You can use this timestamp converter.
And here is a sample response:
Response body
Details on pagination
This section applies to the following metrics output types: Granular project-level metrics for your account, and Billing period project-level metrics for your account.
To control pagination (number of results per response), you can include these query parameters:
limit
— sets the number of project objects to be included in the responsecursor
— by default, the response uses the projectid
from the last project in the list as thecursor
value (included in thepagination
object at the end of the response). Generally, it is up to the application to collect and use this cursor value when setting up the next request.
Here is an example GET
request asking for the next 100 projects, starting with project id divine-tree-77657175
:
note
To learn more about using pagination to control large response sizes, the Keyset pagination page in the Microsoft docs gives a helpful overview.
Last updated on