Skip to main content

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

MethodDescription
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

ExceptionHTTPWhen raised
AgentSimErrorBase class for all SDK errors
AuthenticationError401Missing or invalid API key
ForbiddenError403Key lacks permission for this operation
CountryNotAllowedError403Requested country is not available on the account’s current plan
ValidationError422Invalid request parameters
OtpTimeoutError408No parseable OTP arrived within timeout. Inspect raw messages and support matrix before retrying.
PoolExhaustedError503No numbers available in requested country
ApiError (status 402)402Hobby plan 10-session/month limit exceeded
RateLimitError429Too many requests
SessionNotFoundError404Session ID not found
ApiErrorvariesUnclassified API error