Querying consumption metrics
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 Configure 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 and Business 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 and Business plan only |
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.
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 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: Granular 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 10 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.
Consumption polling FAQ
As an Neon partner or paid plan customer, you may have questions related to polling Neon's consumption APIs. We've provided answers to frequently asked questions here.
How often can you poll consumption data for usage reporting and billing?
Neon's consumption data is updated approximately every 15 minutes, so a minimum interval of 15 minutes between calls to our consumption APIs is recommended.
How often should consumption data be polled to report usage to customers?
As mentioned above, usage data can be pulled every 15 minutes, but partners are free to choose their own reporting interval based on their requirements.
How often should consumption data be polled to invoice end users?
Neon does not dictate how partners bill their users. Partners can use the data retrieved from the consumption API to generate invoices according to their own billing cycles and preferences.
Does consumption polling wake up computes?
Neon's consumption polling APIs do not wake computes that have been suspended due to inactivity. Therefore, calls to Neon's consumption APIs will not increase your users' consumption.