Session class represents an authenticated connection to an AgentDoor-enabled service. It wraps the native fetch API with convenience methods and handles authentication, token refresh, and x402 payment headers automatically.
Overview
Sessions are created by theAgentDoor.connect() method and should not be instantiated directly. Each session maintains:
- Bearer token authentication (with automatic refresh)
- API key fallback for long-lived credentials
- Optional x402 payment header attachment
- Request timeout management
- Response parsing and error handling
Properties
The base URL of the connected service.
Array of scopes granted by the service during registration.
The agent ID assigned by the service.
The discovery document for this service, containing metadata and endpoint information.
Request Methods
All request methods return aSessionResponse<T> object with the parsed response data.
get()
Make an authenticated GET request.The URL path to request (e.g., “/weather/forecast”). Will be joined with the session’s base URL.
Request options including query parameters, headers, and payment settings. The
body option is not available for GET requests.post()
Make an authenticated POST request.The URL path to request.
Request options including body, headers, and payment settings.
put()
Make an authenticated PUT request.The URL path to request.
Request options including body, headers, and payment settings.
delete()
Make an authenticated DELETE request.The URL path to request.
Request options. The
body option is not available for DELETE requests.patch()
Make an authenticated PATCH request.The URL path to request.
Request options including body, headers, and payment settings.
request()
Low-level authenticated request method. All convenience methods delegate to this.HTTP method (GET, POST, PUT, DELETE, PATCH, etc.).
The URL path to request.
Request options.
Request Options
All request methods accept an options object with the following fields:Query parameters to append to the URL. Automatically URL-encoded.
Additional HTTP headers to include in the request. These are merged with default headers.
JSON request body. Automatically serialized and
Content-Type: application/json is set.Whether to attach the x402 payment header. Requires the AgentDoor instance to have been configured with a wallet. Defaults to
false.Request timeout in milliseconds. Defaults to 30000 (30 seconds).
Custom fetch function override for this specific request.
Response Type
All request methods return aSessionResponse<T> object:
HTTP status code (e.g., 200, 404, 500).
HTTP status text (e.g., “OK”, “Not Found”).
Response headers as a Web API Headers object.
Parsed response body. If the response content-type is
application/json, this will be the parsed JSON. Otherwise, it will be the response text.Whether the response status is in the 2xx range.
Authentication
The Session automatically handles authentication by:- Token-based auth - Uses the JWT token from registration with
Authorization: Bearer <token>header - Token refresh - Automatically refreshes expired tokens before requests
- 401 retry - Retries requests once if a 401 response is received (after refreshing)
- API key fallback - Falls back to the long-lived API key if token is unavailable
Payment Headers (x402)
When the session is configured with an x402 wallet, you can attach payment headers to requests:- Wallet address
- Network and currency
- Request signature
- Timestamp
- Maximum payment amount
Error Handling
Sessions throwSessionError exceptions for request failures:
Human-readable error message describing the failure.
HTTP status code (0 if the request failed before receiving a response).