Use the validate_api_key()
method to authenticate User API Keys and understand its use cases before charging for usage.
consume()
method inherently validates a User API Key before attempting a charge, we recommend validating the API Key proactively before processing incoming requests. There are two places where you might choose to do so:
validate_api_key()
Methodapi_key
(str): Required. The User API Key to be validated.validate_api_key()
method returns a ValidationResult
object with the following attributes:
is_valid
(bool): True
if the API key is valid, active, and recognized by AgentPay. False
otherwise.invalid_reason
(str | None): If is_valid
is False
, provides the reason for invalidity. Possible values:
"invalid_key"
: The key is not recognized or has been revoked/deactivated"outstanding_payment"
: The key is associated with an account that has an unpaid consumption attempt from a previous failed consume()
call due to insufficient funds. The key will be automatically reactivated once the user adds sufficient funds to their account."insufficient_balance"
: The key is valid but the associated account has no remaining balance (including free credits)None
if is_valid
is True
"outstanding_payment"
state is a temporary condition that occurs when a user attempts to consume more than their available balance. Once they add sufficient funds to their account, the outstanding payment will be automatically processed and their API key will be reactivated.validate_api_key()
vs. consume()
consume()
: Use this when you are performing a billable action. It implicitly validates the key as part of the consumption process. If the key is invalid, consume()
will fail.validate_api_key()
: Use this for pre-checks if needed:
consume()
also returns error codes for this).validate_api_key()
and then immediately consume()
for every request if not strictly necessary, as this involves two separate calls to the AgentPay system. Rely on consume()
for validation if an operation is definitely billable.