twitterapi.usage module

usage.py: Implementation of Twitter Usage API endpoint.

“The Twitter API usage endpoint provides information about API usage, including rate limits and counts for various endpoints. This is useful for monitoring and optimizing your API usage to avoid exceeding rate limits.” (Source: https://developer.twitter.com/en/docs/twitter-api/usage)

Find the Open API Spec under: https://api.twitter.com/2/openapi.json

Examples

Retrieve usage data for your application:

import os
import asyncio
os.environ["BEARER_TOKEN"] = "xxxxxxxxxxx"
from sparta.twitterapi.usage import get_usage

async def main():
    usage_data = await get_usage()
    if usage_data:
        print("API Usage Information:")
        print(f"Project ID: {usage_data.project_id}")
        print(f"Daily Project Usage: {usage_data.daily_project_usage}")
        print(f"Daily Client App Usage: {usage_data.daily_client_app_usage}")
        print(f"Project Cap: {usage_data.project_cap}")
        print(f"Cap Reset Day: {usage_data.cap_reset_day}")

asyncio.run(main())
async sparta.twitterapi.usage.get_usage() Usage | None

Fetches usage data from the Twitter API.

This function retrieves detailed API usage data for the current application, including rate limits and usage counts for specific endpoints. The usage endpoint provides insights into how the API is being used and allows developers to monitor limits to prevent overages.

Returns:

A Usage object containing detailed API usage information.

Return type:

Optional[Usage]

Raises:

Exception – If the request fails due to an HTTP error or other issue.

Example

>>> usage_data = await get_usage()
>>> if usage_data:
>>>     print(usage_data.project_id, usage_data.daily_project_usage, usage_data.project_cap)