Telegraph API Wrapper

2.2.0 · active · verified Fri Apr 17

The `telegraph` library is a Python wrapper for the Telegraph publishing platform API, allowing programmatic creation and management of Telegraph pages and accounts. It supports both synchronous and asynchronous operations. The current version is 2.2.0, with releases occurring every few months for new features and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

Initializes the Telegraph client, creates a new account if no access token is provided (or uses an existing one), and then publishes a simple page. Ensure `TELEGRAPH_ACCESS_TOKEN` is set as an environment variable to reuse an existing token.

import os
from telegraph import Telegraph

# Create a Telegraph instance (can reuse existing access_token if available)
# For a fresh start, omit access_token to create a new one.
access_token = os.environ.get('TELEGRAPH_ACCESS_TOKEN', None)
telegraph = Telegraph(access_token=access_token)

if not telegraph.get_access_token():
    # If no access token is set, create a new account
    account_info = telegraph.create_account(
        short_name='MyTestAccount',
        author_name='Test User',
        author_url='https://example.com'
    )
    telegraph.access_token = account_info['access_token']
    print(f"New Telegraph account created. Access Token: {telegraph.access_token}")
else:
    print(f"Using existing Telegraph account with Access Token: {telegraph.access_token}")

# Create a new Telegraph page
response = telegraph.create_page(
    title='My First Telegraph Page',
    author_name='Test User',
    content='<p>Hello from <b>Python</b>!</p><p>This is a test page created programmatically.</p>',
    return_content=True
)

print(f"Page created: {response['url']}")
print(f"Page views: {response['views']}")

view raw JSON →