Skip to main content

NewsMesh Python SDK

Official Python SDK for the NewsMesh News API.

Installation

pip install newsmesh

For async support:

pip install newsmesh[async]

Quick Start

from newsmesh import NewsMeshClient

client = NewsMeshClient("your-api-key")

# Get trending articles
trending = client.get_trending(limit=10)
for article in trending:
    print(f"{article.title} - {article.source}")

Features

  • Sync and Async clients - Choose based on your application needs
  • Type hints - Full type annotations for IDE support
  • Automatic retries - Exponential backoff on transient failures
  • Pagination helpers - Easy iteration through large result sets
  • Custom exceptions - Granular error handling

Usage

Initialize the Client

from newsmesh import NewsMeshClient

# Basic initialization
client = NewsMeshClient("your-api-key")

# With custom settings
client = NewsMeshClient(
    "your-api-key",
    timeout=60.0,      # Request timeout in seconds
    max_retries=5,     # Max retry attempts
)

# Using context manager (recommended)
with NewsMeshClient("your-api-key") as client:
    articles = client.get_trending()
response = client.get_trending(limit=10)

for article in response:
    print(article.title)
    print(article.description)
    print(article.link)
    print("---")

Get Latest Articles

# Basic usage
response = client.get_latest(limit=25)

# With filters
response = client.get_latest(
    category="technology",           # Single category
    country="us",                    # ISO 3166-1 alpha-2 code
    limit=10,
)

# Multiple categories and countries
response = client.get_latest(
    category=["politics", "world"],
    country=["us", "gb"],
)

# With date range
from datetime import date

response = client.get_latest(
    from_date="2025-01-01",          # String format
    to_date=date(2025, 1, 31),       # Or date object
)

Search Articles

# Basic search
response = client.search("climate change")

# With options
response = client.search(
    "artificial intelligence",
    limit=20,
    sort_by="relevant",              # "date_descending", "date_ascending", "relevant"
    from_date="2025-01-01",
)

Get Single Article

article = client.get_article("article-id-123")
print(article.title)

Pagination

# Manual pagination
response = client.get_latest(limit=25)
while response.has_more:
    for article in response:
        print(article.title)
    response = client.get_latest(cursor=response.next_cursor)

# Automatic pagination with iterator
for article in client.paginate("get_latest", category="technology"):
    print(article.title)

Async Usage

import asyncio
from newsmesh import AsyncNewsMeshClient

async def main():
    async with AsyncNewsMeshClient("your-api-key") as client:
        # Get trending
        trending = await client.get_trending()

        # Search
        results = await client.search("tech news")

        # Paginate
        async for article in client.paginate("get_latest"):
            print(article.title)

asyncio.run(main())

Error Handling

from newsmesh import (
    NewsMeshClient,
    AuthenticationError,
    RateLimitError,
    NotFoundError,
    ValidationError,
)

client = NewsMeshClient("your-api-key")

try:
    article = client.get_article("invalid-id")
except AuthenticationError:
    print("Invalid API key")
except RateLimitError as e:
    print(f"Rate limited. Retry after: {e.retry_after}")
except NotFoundError:
    print("Article not found")
except ValidationError as e:
    print(f"Invalid request: {e.message}")

Article Properties

article = client.get_article("some-id")

article.article_id       # Unique identifier
article.title            # Headline
article.description      # Summary/blurb
article.link             # URL to original article
article.published_date   # ISO 8601 timestamp string
article.published_datetime  # As datetime object
article.source           # Publisher name
article.category         # Category (e.g., "technology")
article.topics           # List of topics/tags
article.media_url        # Featured image URL (may be None)
article.people           # People mentioned (may be None)
article.author           # Author name (may be None)

API Reference

Method Description
get_trending(limit) Get trending articles
get_latest(limit, category, country, from_date, to_date, cursor) Get latest articles with filters
list_articles(...) List articles (alias for get_latest)
get_article(article_id) Get single article by ID
search(query, limit, from_date, to_date, sort_by, cursor) Full-text search
paginate(method, **kwargs) Auto-paginate through results

Supported Countries

Filter by ISO 3166-1 alpha-2 codes:

ae, au, be, br, ca, cn, co, de, eg, fr, gb, hk, id, ie, il, in, it, jp, kr, mx, my, ng, nl, no, ph, ru, sa, sg, th, tr, tw, ua, us, ve, za

License

MIT

Metadata

Release files for newsmesh 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 newsmesh 0.1.0
File Size Uploaded
newsmesh-0.1.0.tar.gz 15.5 kB Details

Built distribution (wheel)

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

Total release size: 31.6 kB

Release files / newsmesh-0.1.0.tar.gz

Download URL newsmesh-0.1.0.tar.gz
Size 15.5 kB
Tags Source
SHA-256 checksum
How to use checksums
06b6bde33bfb0ab11499aefc6a312ab1bc29411909607140801fe6f930b259d2
BLAKE2b-256 checksum
How to use checksums
92260d01a1bbe9b8693556e9ab21f623db5280ddcccb31e97ec876947e20132c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

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

Download URL newsmesh-0.1.0-py3-none-any.whl
Size 16.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
288e48ae08b26e7c7e7b88e673351515bef3f2bee92dd7cee392d1cf86ddca9b
BLAKE2b-256 checksum
How to use checksums
65095a4b2cb2e0391aaa285a446c7884491a35ffb78645f49100c74c3f384208
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

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