Skip to main content

Official Python SDK for SocialAPI -- unified social media inbox API

Project description

SocialAPI Python SDK

CI PyPI version Python License: MIT

The official Python client for the SocialAPI REST API -- a unified inbox for comments, DMs, reviews, mentions, and publishing across Instagram, Facebook, Threads, TikTok, Google Business Profile, and LinkedIn.

Installation

pip install socialapi

Quick Start

from socialapi import SocialAPI

client = SocialAPI(api_key="sapi_key_...")

# List connected accounts
for account in client.accounts.list():
    print(f"{account.platform}: {account.account_name}")

# Reply to a comment
client.comments.reply(post_id="ip_123", text="Thanks for the feedback!")

# Publish a post to multiple platforms
post = client.publishing.create(
    text="Hello from the API!",
    platforms=[{"platform": "instagram", "account_id": "acc_ig_001"}],
)

Authentication

Pass your API key directly or set it as an environment variable:

# Option 1: Constructor
client = SocialAPI(api_key="sapi_key_...")

# Option 2: Environment variable
#   export SOCIALAPI_API_KEY=sapi_key_...
client = SocialAPI()

You can generate API keys from the SocialAPI dashboard.

Async Support

Every method has an async equivalent via AsyncSocialAPI:

import asyncio
from socialapi import AsyncSocialAPI

async def main():
    async with AsyncSocialAPI(api_key="sapi_key_...") as client:
        accounts = await client.accounts.list()
        async for account in accounts:
            print(account.account_name)

asyncio.run(main())

Usage Examples

Comments

# List posts with comments
posts = client.comments.list_posts(platform="instagram")

# List comments on a specific post
comments = client.comments.list(post_id="ip_123")

# Moderate
client.comments.hide(post_id="ip_123", comment_id="cmt_456")
client.comments.like(post_id="ip_123", comment_id="cmt_456")
client.comments.delete(post_id="ip_123", comment_id="cmt_456")

Direct Messages

# List conversations
conversations = client.conversations.list(platform="instagram")

# Read messages in a thread
messages = client.conversations.list_messages(conversation_id="conv_001")

# Reply
client.conversations.send_message(conversation_id="conv_001", text="Hi there!")
client.conversations.mark_as_read(conversation_id="conv_001")

Reviews

# List reviews across Google Business Profile
reviews = client.reviews.list(platform="google")

# Reply to a review
client.reviews.reply(review_id="rev_789", text="Thank you for the kind words!")

Publishing

# Create and schedule a post
from datetime import datetime, timezone

post = client.publishing.create(
    text="Scheduled post!",
    platforms=[
        {"platform": "instagram", "account_id": "acc_ig_001"},
        {"platform": "facebook", "account_id": "acc_fb_002"},
    ],
    scheduled_at=datetime(2026, 4, 1, 12, 0, tzinfo=timezone.utc).isoformat(),
)

# Validate before publishing
result = client.publishing.validate(
    text="Check this post",
    platforms=["instagram"],
    account_ids=["acc_ig_001"],
)
if not result.valid:
    for issue in result.errors:
        print(f"{issue.platform}: {issue.message}")

Webhooks

# Subscribe to events
webhook = client.webhooks.create(
    url="https://example.com/webhook",
    events=["comment.received", "dm.received"],
)

# Update
client.webhooks.update(webhook.id, is_active=False)

Pagination

List endpoints return a CursorPage that auto-paginates:

# Iterate through all pages automatically
for post in client.posts.list(status="published"):
    print(post.text)

# Or control pagination manually
page = client.posts.list(limit=10)
print(f"Page has {len(page)} items, has_more={page.has_more}")
if page.has_more:
    next_page = page.next_page()

Error Handling

The SDK raises typed exceptions mapped to API error codes:

from socialapi import (
    SocialAPIError,
    AuthenticationError,
    NotFoundError,
    RateLimitError,
)

try:
    client.comments.reply(post_id="ip_123", text="Hello")
except AuthenticationError:
    print("Invalid API key")
except NotFoundError:
    print("Post not found")
except RateLimitError as e:
    print(f"Rate limited: retry after checking headers")
except SocialAPIError as e:
    print(f"API error {e.status_code}: {e.message}")
HTTP Status Exception When
400 BadRequestError Invalid parameters
401 AuthenticationError Bad or missing API key
404 NotFoundError Resource doesn't exist
409 ConflictError Account already linked
413 StorageQuotaExceededError Storage quota exceeded
429 RateLimitError Rate limit hit
500 InternalServerError Server error
501 NotSupportedError Platform doesn't support this

Network and timeout errors raise APIConnectionError and APITimeoutError.

Configuration

client = SocialAPI(
    api_key="sapi_key_...",
    base_url="https://api.social-api.ai",  # default
    timeout=30.0,                           # seconds, default
    max_retries=2,                          # default
    debug=False,                            # log raw HTTP requests/responses
)

# Per-request timeout override
post = client.posts.get(post_id="p_123", timeout=60.0)

The client automatically retries on 429 (rate limit) and 5xx errors with exponential backoff and jitter. The Retry-After header is respected when present.

Resource Cleanup

Always close the client when done, or use it as a context manager:

# Context manager (recommended)
with SocialAPI(api_key="...") as client:
    client.posts.list()

# Manual close
client = SocialAPI(api_key="...")
try:
    client.posts.list()
finally:
    client.close()

Typed Enums

The SDK exports typed enums for API constants, enabling IDE autocomplete:

from socialapi import Platform, PostStatus, WebhookEvent

# Use in filters
posts = client.posts.list(platform=Platform.INSTAGRAM, status=PostStatus.PUBLISHED)

# Use in webhook subscriptions
client.webhooks.create(url="...", events=[WebhookEvent.COMMENT_RECEIVED])

API Reference

See the full API documentation at docs.social-api.ai.

Contributing

See CONTRIBUTING.md for development setup and guidelines.

License

MIT -- see LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

socialapi-0.2.0.tar.gz (53.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

socialapi-0.2.0-py3-none-any.whl (50.0 kB view details)

Uploaded Python 3

File details

Details for the file socialapi-0.2.0.tar.gz.

File metadata

  • Download URL: socialapi-0.2.0.tar.gz
  • Upload date:
  • Size: 53.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for socialapi-0.2.0.tar.gz
Algorithm Hash digest
SHA256 dd8d0724005cdd147ae2e8a538a21b294b148a84c332bb6e905a01c8e763c581
MD5 afd915f5266582b0de2986a8470cf15e
BLAKE2b-256 0784e1f84822bc2cd0643c625f4bc3b2ad52023bb4e3f5cfe81f8cdf23ab73fa

See more details on using hashes here.

Provenance

The following attestation bundles were made for socialapi-0.2.0.tar.gz:

Publisher: publish.yml on SocialAPI-AI/socialapi-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file socialapi-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: socialapi-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 50.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for socialapi-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c95e93410ac70dc31a5284b9c4566b23acedeb7d955a1bb3904b2d38658bb205
MD5 3ffa8dd3b215f311a8ba607952872244
BLAKE2b-256 8ccdcac0dab3e5f7c8366ca3fa9741645c3111e31a9c6d522a86c75c14c2f004

See more details on using hashes here.

Provenance

The following attestation bundles were made for socialapi-0.2.0-py3-none-any.whl:

Publisher: publish.yml on SocialAPI-AI/socialapi-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page