How does Stainless handle OAuth 2.0 authentication?
Last updated: June 24, 2025
Context
When implementing an API that uses OAuth 2.0 authentication (either 2-legged client credentials or 3-legged OAuth), developers need to understand how Stainless SDKs handle token management and authentication.
Answer
While Stainless SDKs do not currently generate OAuth token management code automatically, they do provide the necessary infrastructure to implement OAuth authentication in your applications.
Stainless SDKs support:
Configuration options on the SDK client for passing tokens, keys, and secrets that will be attached to requests
Methods on the SDK client for any endpoint, which can be used to expose methods for fetching access tokens
Pre-request hooks that allow you to implement custom token retrieval and refresh logic
To implement OAuth 2.0 in your application:
Handle the OAuth flow separately using existing OAuth libraries (see OAuth SDKs)
Pass the resulting tokens to your Stainless SDK
Implement token refresh logic using the pre-request hooks available in each language SDK
Examples of implementing pre-request hooks for token management:
TypeScript: Use the beforeRequest hook in the core client
Python: Implement the prepare_request method in the base client
Java: Utilize the PrepareRequest interface
For more details on authentication configuration, refer to the Authentication documentation.