Installation
uv add agentsim-sdk
# or: pip install agentsim-sdk
Requires Python 3.11+.
Authentication
Set the AGENTSIM_API_KEY environment variable:
export AGENTSIM_API_KEY="asm_live_..."
Or configure explicitly in code:
import agentsim
agentsim.configure(api_key="asm_live_...")
Methods
| Method | Description |
|---|
agentsim.configure(api_key, base_url?) | Set module-level API key and base URL |
agentsim.provision(agent_id, country?, service_url?, ttl_seconds?, webhook_url?) | Async context manager: provisions a number, auto-releases on exit |
agentsim.provision_sync(agent_id, country?, service_url?, ttl_seconds?, webhook_url?) | Sync context manager: same as above for non-async code |
num.wait_for_otp(timeout?, auto_reroute?, max_reroutes?, on_reregistration_needed?) | Async: waits for an OTP, returns OtpResult. Set auto_reroute=True to provision a replacement number in another country on timeout. |
num.wait_for_otp_sync(timeout?) | Sync variant of wait_for_otp |
num.messages() | Async: returns list[SmsMessage] — all SMS received on this session |
num.release() | Manually release the number (called automatically on context exit) |
provision() starts a billable session after the free tier. $0.99 per session on the Builder plan. OtpTimeoutError means no parseable OTP arrived within the wait window; inspect raw messages and destination support before retrying.
Full example
import asyncio
import agentsim
import os
agentsim.configure(api_key=os.environ["AGENTSIM_API_KEY"])
async def main():
try:
async with agentsim.provision(
agent_id="stripe-onboarding",
country="US",
ttl_seconds=300,
) as num:
print(f"Provisioned: {num.number}")
# Enter num.number into the service that will send the OTP...
otp = await num.wait_for_otp(timeout=60)
print(f"OTP: {otp.otp_code}")
except agentsim.OtpTimeoutError:
print("No OTP arrived within 60 seconds. Inspect raw messages and support matrix before retrying.")
except agentsim.PoolExhaustedError:
print("No numbers available in US right now. Try again shortly.")
except agentsim.ApiError as e:
if e.status_code == 402:
print("Hobby plan 10-session limit reached. Upgrade to Builder.")
asyncio.run(main())
Sync example
import agentsim, os
agentsim.configure(api_key=os.environ["AGENTSIM_API_KEY"])
with agentsim.provision_sync(agent_id="checkout-bot") as num:
print(f"Number: {num.number}")
otp = num.wait_for_otp_sync(timeout=60)
print(f"OTP: {otp.otp_code}")
Exception hierarchy
| Exception | HTTP | When raised |
|---|
AgentSimError | — | Base class for all SDK errors |
AuthenticationError | 401 | Missing or invalid API key |
ForbiddenError | 403 | Key lacks permission for this operation |
CountryNotAllowedError | 403 | Requested country is not available on the account’s current plan |
ValidationError | 422 | Invalid request parameters |
OtpTimeoutError | 408 | No parseable OTP arrived within timeout. Inspect raw messages and support matrix before retrying. |
PoolExhaustedError | 503 | No numbers available in requested country |
ApiError (status 402) | 402 | Hobby plan 10-session/month limit exceeded |
RateLimitError | 429 | Too many requests |
SessionNotFoundError | 404 | Session ID not found |
ApiError | varies | Unclassified API error |