memory-bank/components/usage-api.md
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:
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 centerProvides 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 providerRepresents a cost center configuration:
Represents a usage entry:
Represents a Stripe customer:
Represents a Stripe subscription:
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:
The buf.gen.yaml file configures the code generation:
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.