The Usage API defines the gRPC interfaces for the Usage service, which is responsible for tracking, calculating, and managing workspace usage, billing, and credits within the Gitpod platform. This API enables the management of cost centers, usage tracking, and integration with payment providers like Stripe.
This API provides a standardized interface for:
- Tracking workspace usage and associated credits
- Managing cost centers and spending limits
- Reconciling usage data for billing purposes
- Integrating with payment providers (primarily Stripe)
- Managing customer subscriptions and payment methods
- Handling billing-related operations like invoices and disputes
The Usage API is implemented as a set of gRPC services defined in Protocol Buffer files. These definitions are used to generate client and server code in Go and TypeScript for use by the usage service and other components in the system.
Provides methods for managing usage and credits:
GetCostCenter
: Retrieves the active cost center for a given attribution IDSetCostCenter
: Stores a cost center configurationReconcileUsage
: Triggers reconciliation of usage dataResetUsage
: Resets usage for cost centers that have expired or will expire soonListUsage
: Retrieves all usage for a specified attribution ID and time rangeGetBalance
: Returns the current credits balance for a given attribution IDAddUsageCreditNote
: Adds a usage credit note to a cost center
Provides methods for managing billing and payment integration:
ReconcileInvoices
: Retrieves current credit balance and reflects it in the billing systemFinalizeInvoice
: Marks sessions as having been invoicedCancelSubscription
: Cancels a Stripe subscriptionGetStripeCustomer
: Retrieves a Stripe customerCreateStripeCustomer
: Creates a new Stripe customerCreateHoldPaymentIntent
: Creates a payment intent to verify a payment methodCreateStripeSubscription
: Creates a new Stripe subscriptionUpdateCustomerSubscriptionsTaxState
: Updates tax state for customer subscriptionsGetPriceInformation
: Returns price information for a given attribution IDOnChargeDispute
: Handles charge disputes with the payment provider
Represents a cost center configuration:
- Attribution ID
- Spending limit
- Billing strategy (Stripe or Other)
- Next billing time
- Billing cycle start
Represents a usage entry:
- ID
- Attribution ID
- Description
- Credits
- Effective time
- Kind (Workspace Instance or Invoice)
- Workspace instance ID
- Draft status
- Metadata
Represents a Stripe customer:
- ID
- Currency
- Billing address validity
Represents a Stripe subscription:
- ID
- The API uses gRPC for efficient, typed communication
- Requests include attribution IDs to identify the relevant account
- Pagination is supported for listing operations
- Time ranges are used to specify periods for usage data
- Integrated with Stripe for payment processing
- Used by the server component for user billing management
- Used by the workspace manager to track workspace usage
- Used by administrative tools for billing management
- Workspace creation process uses this API to check credit availability
- Billing system uses this API to generate invoices
- Administrative interfaces use this API to manage user credits
- Reporting tools use this API to analyze usage patterns
The API uses Protocol Buffers version 3 (proto3) syntax, which provides forward and backward compatibility features. The service is designed to allow for the addition of new billing options and features without breaking existing clients.
The Usage API uses Protocol Buffers and gRPC for defining interfaces. When changes are made to the .proto
files, the corresponding code in Go and TypeScript needs to be regenerated.
To regenerate the code:
-
Navigate to the usage-api directory:
cd components/usage-api
-
Run the generate script:
./generate.sh
This script performs the following actions:
- Installs necessary dependencies (protoc plugins)
- Lints the proto files using buf
- Generates Go and TypeScript code using buf
- Updates license headers
The generate.sh
script uses functions from the shared script at scripts/protoc-generator.sh
:
install_dependencies
: Installs required protoc pluginslint
: Lints the proto files using bufprotoc_buf_generate
: Generates code using buf based on the configuration inbuf.gen.yaml
update_license
: Updates license headers in generated files
The buf.gen.yaml
file configures the code generation:
- Generates Go code with appropriate module paths
- Generates TypeScript code using ts_proto with specific options for nice-grpc compatibility
After regenerating the code, you may need to rebuild components that depend on the Usage API. This typically involves:
-
For Go components:
cd <component-directory> go build ./...
-
For TypeScript components:
cd <component-directory> yarn install yarn build
-
Using Leeway (for CI/CD):
leeway build -D components/<component-name>:app
The Usage API is primarily used by the usage component, which manages workspace usage tracking, billing, and credits within the Gitpod platform. It plays a critical role in the business operations of the platform by enabling usage-based billing and credit management.