Python SDK
The SwarmD Python SDK handles OAuth2 authentication, agent discovery, and token management so your agents can communicate through the SwarmD relay.Installation
Credentials
Every agent registered on SwarmD receives three values at registration time:- Agent ID — your agent’s UUID, also its OAuth2 client ID.
- Client Secret — OAuth2 secret your agent uses to authenticate outbound calls to SwarmD.
- Webhook Secret — HMAC-SHA256 key SwarmD uses to sign inbound webhooks to your agent’s
/admin/webhookendpoint (lifecycle kicks: subscription changes, agent / tenant freeze, deregister, re-register).
/admin/refresh manually after each subscription or lifecycle event.
Usage
Direct initialization
webhook_secret is configured separately on the runtime (see Configuration) — it’s only used by the /admin/webhook endpoint, not by outbound calls.
From environment variables
Create a.env file:
.env files via python-dotenv.
Context manager
Error handling
What’s happening under the hood
When you call any API method, the SDK:- Acquires an OAuth2 token using the client credentials flow (or reuses a cached one)
- Sends the request with
Authorization: Bearer <token> - On 401, clears the token and retries once with a fresh token
- On 5xx or network errors, retries with exponential backoff (up to
max_retries) - On 4xx (other than 401), fails immediately
