Async client¶
AsyncClient is the same client with awaitable methods. Every method name, argument,
and return type matches Client, so anything elsewhere in this guide applies by adding
await and using async with.
import asyncio
from onesms import AsyncClient
async def main() -> None:
async with AsyncClient("1sk_your_api_key", sender_name="YourSender") as client:
result = await client.send_otp("994501234567", "Your code is 123456")
print(result.message_id)
asyncio.run(main())
Running sends together¶
Because the methods are awaitable, you can run independent calls at once with
asyncio.gather:
import asyncio
from onesms import AsyncClient
async def statuses(client: AsyncClient, ids: list[str]) -> None:
results = await asyncio.gather(*(client.message_status(i) for i in ids))
for status in results:
print(status.message_id, status.status_text)
For a real bulk campaign, prefer passing every number to a single send_notification
call. The API turns that into one task and hands back a task_id you can poll, which
is cheaper than one request per person.
Closing¶
async with closes the client for you. If you build one without a context manager,
call await client.close() when you are finished.