I built a Python library that handles social media posting across multiple providers with automatic fallback. If one API fails, it seamlessly switches to the backup. Here's why and how.
The Problem
When building a social media scheduling tool, I hit a common pain point: API reliability. Each social posting service has its quirks:
- Rate limits
- Occasional downtime
- Different pricing tiers
Relying on a single provider felt risky. What if they go down right when your scheduled post needs to go out?
The Solution: Multi-Provider Architecture
I created social-posting-api - a unified interface that:
- Tries the primary provider first (PostForMe - cheaper, volume-friendly)
- Automatically falls back to LATE if the primary fails
- Uses a consistent API regardless of which provider succeeds
from social_posting import SocialPostingClient
client = SocialPostingClient()
# Posts via PostForMe, falls back to LATE if needed
result = client.post(
content="Hello from my multi-provider API!",
platforms=["linkedin", "instagram"],
media_urls=["https://example.com/image.jpg"]
)
print(f"Posted via: {result.provider}") # "PostForMe" or "LATE"
Key Features
Automatic Fallback
# Try each provider with fallback
for provider in self.providers:
result = provider.post(content, platforms, media_urls)
if result.success:
return result
last_error = result.error
return PostResult(success=False, error=f"All providers failed: {last_error}")
Unified Account Management
# Get connected accounts from any provider
accounts = client.get_accounts()
for account in accounts:
print(f"{account.platform}: {account.username}")
Media Upload with Presigned URLs
Both providers use presigned URL patterns for secure media uploads:
def upload_media(self, image_url: str) -> Optional[str]:
# Download image
img_data = requests.get(image_url).content
# Get presigned upload URL from provider
upload_url = self._get_presigned_url()
# Upload directly to cloud storage
requests.put(upload_url, data=img_data)
return hosted_url
Multi-Image Carousel Support
Instagram carousels work seamlessly:
result = client.post(
content="Check out these photos!",
platforms=["instagram"],
media_urls=[
"https://example.com/photo1.jpg",
"https://example.com/photo2.jpg",
"https://example.com/photo3.jpg"
]
)
Supported Platforms
- Twitter/X
- Instagram (requires media)
- TikTok
- Threads
- Bluesky
- YouTube
Getting Started
git clone https://github.com/PHY041/social-posting-api
cd social-posting-api
pip install -r requirements.txt
Set your API keys:
# .env
POSTFORME_API_KEY=your_key_here
LATE_API_KEY=your_key_here
Run the test:
python social_posting.py
Why Open Source?
I'm building CanMarket, an AI-powered marketing tool. This social posting module is one piece of the puzzle, and I figured others might find it useful.
If you're building anything that needs reliable social media posting, feel free to use this as a starting point!
Links
- GitHub: https://github.com/PHY041/social-posting-api
- PostForMe API: https://postforme.dev
- LATE API: https://getlate.dev
Got questions or suggestions? Drop a comment below!
Top comments (0)