Skip to main content

SocialLens Python SDK

Official Python client for the SocialLens API. It reads public profile, post and comment data from 9 social networks (Instagram, TikTok, YouTube, X, Facebook, LinkedIn, Threads, Pinterest and Reddit) and returns the API's JSON as plain dictionaries.

  • One request format for every network.
  • Public data only. No login to any social account.
  • Pay as you go: 1 credit per delivered read, and errors are never charged. A new account starts with 250 free reads, no card.

Install

pip install sociallens

Python 3.8 or newer. The only dependency is requests.

Quickstart

from sociallens import SocialLens

client = SocialLens("YOUR_API_KEY")  # or set SOCIALLENS_API_KEY and call SocialLens()

profile = client.profile("instagram", "nasa")
print(profile["followers"], profile["is_exact"])

post = client.post("tiktok", "https://www.tiktok.com/@tiktok/video/7106594312292453675")
print(post["likes"], post["views"], post["comments"])

page = client.profile_posts("youtube", "mkbhd", limit=5)
for item in page["posts"]:
    print(item["published_at"], item["likes"], item["views"])

Get a free key at https://sociallens.dev/register.

Methods

Method Endpoint Networks
ping() GET /v1/ping Checks the key. Free
profile(platform, username) GET /v1/profile All 9
post(platform, url) GET /v1/post All except LinkedIn
profile_posts(platform, username, limit=10) GET /v1/profile-posts All 9
post_comments(platform, url, limit=20) GET /v1/post-comments TikTok, Instagram, YouTube, Facebook, Reddit
followers(username, cursor=None) GET /v1/followers TikTok
following(username, cursor=None) GET /v1/following TikTok

platform is one of tiktok, instagram, youtube, facebook, x, reddit, linkedin, threads, pinterest. The same list is exported as sociallens.PLATFORMS, and sociallens.COVERAGE maps each method to the networks it supports.

Optional keyword arguments on the read methods:

  • fresh=True skips the short read cache.
  • cache_max_age="1d" (or 3d, 7d, 14d, 30d) lets the source answer from its own stored copy.
  • type="person" on profile reads a LinkedIn person when the username is a name. The default is a company. A full LinkedIn URL needs no type.

username is the public handle, with or without the @. On Reddit it is the subreddit name. Facebook and LinkedIn also accept the full URL.

Reading the result

Every method returns the JSON body as a dict, unchanged. The field reference is in the API documentation.

profile = client.profile("tiktok", "some_handle")

if not profile["exists"]:
    print("No such profile")       # a delivered answer, not an error
elif profile.get("private"):
    print("Private profile")
else:
    print(profile["followers"])    # exact when profile["is_exact"] is True
  • exists: false is a result, not an exception.
  • is_exact is False when the network only publishes a rounded number, for example YouTube subscribers.
  • A counter the network does not publish is None, never 0.
  • fetched_at is the time of the read, in UTC.
  • credits shows what the call charged and the balance left.

Two networks answer profile with a different shape. client.profile("reddit", "science") returns a subreddit with active_users (weekly) and weekly_contributions, because Reddit has no follower count. client.profile("linkedin", "shopify") returns a company with followers and employee_count.

Errors

An error status raises an exception. No error status is ever charged.

Exception Status and code
BadRequestError 400 bad_request
AuthenticationError 401 invalid_token
InsufficientCreditsError 402 no_credits, free_tier_exhausted
PermissionDeniedError 403 email_not_verified
AccountBusyError 409 account_busy
RateLimitError 429 rate_limited
NotAvailableError 501 platform_not_available
SourceUnavailableError 503 source_unavailable
APIConnectionError No HTTP answer: network error or timeout

All HTTP errors inherit from APIError and carry status, code, message, body and retry_after. Everything inherits from SocialLensError.

from sociallens import APIError, RateLimitError, SocialLens

client = SocialLens("YOUR_API_KEY")

try:
    data = client.profile("tiktok", "nasa")
except RateLimitError as error:
    print("Retry in", error.retry_after, "seconds")
except APIError as error:
    print(error.status, error.code, error.message)

Retries, timeouts and pace

  • 409, 429 and 503 are retried up to max_retries times (default 2), waiting the Retry-After the API sends. Those statuses are never charged.
  • Network errors and timeouts raise APIConnectionError and are not retried, because the read may have completed on the server.
  • The API processes the requests of one account one at a time and allows 60 requests per minute per key. Run reads in sequence: a parallel pool does not finish sooner.
client = SocialLens("YOUR_API_KEY", timeout=90, max_retries=3)

Paging a TikTok follower list

Each page is one credit, so set your own ceiling.

cursor, pages = None, 0
while pages < 3:
    page = client.followers("nasa", cursor=cursor)
    for user in page["users"]:
        print(user["username"], user["followers"])
    pages += 1
    if not page["has_more"]:
        break
    cursor = page["next_cursor"]

Billing

A delivered answer (HTTP 200) costs 1 credit, and every error costs nothing. For profiles, "does not exist" and "private" are delivered answers. The rules for posts that are not found differ by network and are listed in the documentation.

License

MIT

Metadata

Release files for sociallens 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sociallens 0.1.0
File Size Uploaded Provenance Actions
sociallens-0.1.0.tar.gz 12.5 kB Without provenance Details for sociallens-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sociallens 0.1.0
File Interpreter ABI Platform Actions
sociallens-0.1.0-py3-none-any.whl Python 3 none any Details for sociallens-0.1.0-py3-none-any.whl

Total release size: 22.5 kB

Release files / sociallens-0.1.0.tar.gz

Download URL sociallens-0.1.0.tar.gz
Size 12.5 kB
Tags Source
SHA-256 checksum
How to use checksums
8f75adcf8dd513983d4e3b9a1cb0da3a80e7bea69e194cb85d06a0f06b798e50
BLAKE2b-256 checksum
How to use checksums
a117e7034e4ed78eecc0edbee95afd48cf7509fc278a11154c2c4a798e2cf1bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / sociallens-0.1.0-py3-none-any.whl

Download URL sociallens-0.1.0-py3-none-any.whl
Size 9.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5bbabe139bfe971fa8037b086048d314a8b9800d88ed72ad21c45390fc254571
BLAKE2b-256 checksum
How to use checksums
502f69a8da5a790e72863613d22cbc777f65ca30700f9dc642acd4bef63c3b57
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Security for sociallens 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page