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=Trueskips the short read cache.cache_max_age="1d"(or3d,7d,14d,30d) lets the source answer from its own stored copy.type="person"onprofilereads a LinkedIn person when the username is a name. The default is a company. A full LinkedIn URL needs notype.
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: falseis a result, not an exception.is_exactisFalsewhen the network only publishes a rounded number, for example YouTube subscribers.- A counter the network does not publish is
None, never0. fetched_atis the time of the read, in UTC.creditsshows 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,429and503are retried up tomax_retriestimes (default 2), waiting theRetry-Afterthe API sends. Those statuses are never charged.- Network errors and timeouts raise
APIConnectionErrorand 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.
Links
- API documentation: https://sociallens.dev/docs
- Pricing: https://sociallens.dev/pricing
- Guides: https://sociallens.dev/blog
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)
| 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)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| 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
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 files / sociallens-0.1.0.tar.gz
How to use checksums
8f75adcf8dd513983d4e3b9a1cb0da3a80e7bea69e194cb85d06a0f06b798e50How to use checksums
a117e7034e4ed78eecc0edbee95afd48cf7509fc278a11154c2c4a798e2cf1bbWhat is trusted publishing?
twine/7.0.0 CPython/3.12.3