Python SDKPython SDK Getting Started
Python SDK Getting Started
Install and configure the SocialRails Python SDK with sync and async support.
The official Python SDK for the SocialRails API. Supports both synchronous and async usage with full type hints.
Installation
pip install socialrailsRequires Python 3.8+ and httpx >= 0.24.0.
Quick Start
from socialrails import SocialRails
client = SocialRails(api_key="sr_live_...")
post = client.posts.create(
content="Launching our new feature today!",
platform="twitter",
scheduled_for="2026-03-15T14:30:00Z",
)
print(post["id"])
client.close()Async Usage
import asyncio
from socialrails import AsyncSocialRails
async def main():
async with AsyncSocialRails(api_key="sr_live_...") as client:
post = await client.posts.create(
content="Hello from async Python!",
platform="linkedin",
)
print(post["id"])
asyncio.run(main())Configuration
The constructor checks for SOCIALRAILS_API_KEY when no api_key argument is provided:
import os
os.environ["SOCIALRAILS_API_KEY"] = "sr_live_..."
client = SocialRails() # picks up the env varContext Manager
Both clients support context managers for automatic cleanup:
# Sync
with SocialRails(api_key="sr_live_...") as client:
posts = client.posts.list()
# Async
async with AsyncSocialRails(api_key="sr_live_...") as client:
posts = await client.posts.list()Error Handling
All API errors raise SocialRailsError with message, code, and status attributes:
from socialrails import SocialRails, SocialRailsError
client = SocialRails(api_key="sr_live_...")
try:
client.posts.create(content="", platform="twitter")
except SocialRailsError as e:
print(f"Error {e.status}: [{e.code}] {e.message}")Next Steps
See the Python SDK Reference for the full API reference.