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 socialrails

Requires 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

OptionEnvironment variableDescription
api_keySOCIALRAILS_API_KEYYour API key (starts with sr_live_). Required.
base_urlAPI base URL. Defaults to https://socialrails.com/api/v1.

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 var

Context 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}")
CodeStatusDescription
BAD_REQUEST400Invalid request parameters
UNAUTHORIZED401Missing or invalid API key
FORBIDDEN403API key lacks required scope
NOT_FOUND404Resource not found
RATE_LIMITED429Too many requests
LIMIT_EXCEEDED429Monthly plan limit reached
PAYLOAD_TOO_LARGE413Request body or file too large
INTERNAL_ERROR500Server error

Next Steps

See the Python SDK Reference for the full API reference.