Expo Server SDK for Python

2.2.0 · active · verified Tue Apr 14

The `exponent-server-sdk` is a community-maintained Python library that provides a convenient way to send push notifications to mobile applications built with Expo. It wraps the Expo Push Notification Service API, allowing Python servers to interact with Expo experiences. The current version is 2.2.0, with an irregular release cadence driven by community contributions and upstream Expo API changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to send a push notification using `exponent-server-sdk`. It initializes a `requests` session with an optional Expo access token for push security, constructs a `PushMessage` with a target Expo push token, title, body, and optional data, and publishes it using `PushClient`. It includes basic error handling for common scenarios like device unregistration or server errors. Ensure `EXPO_TOKEN` and a valid `EXPO_PUSH_TOKEN` are set as environment variables.

import os
import requests
from exponent_server_sdk import (
    PushClient,
    PushMessage,
    DeviceNotRegisteredError,
    PushServerError,
)
from requests.exceptions import ConnectionError, HTTPError

EXPO_ACCESS_TOKEN = os.environ.get('EXPO_TOKEN', '') # Get from Expo dashboard
EXPO_PUSH_TOKEN = os.environ.get('EXPO_PUSH_TOKEN', 'ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]') # A device-specific token

session = requests.Session()
session.headers.update(
    {
        "Authorization": f"Bearer {EXPO_ACCESS_TOKEN}",
        "Accept": "application/json",
        "Accept-Encoding": "gzip, deflate",
        "Content-Type": "application/json",
    }
)

def send_expo_push_message(token, title, body, data=None):
    try:
        response = PushClient(session=session).publish(
            PushMessage(
                to=token,
                title=title,
                body=body,
                data=data
            )
        )
        response.validate_response()
        print(f"Push message sent successfully: {response.json()}")
    except DeviceNotRegisteredError:
        print(f"Error: Device {token} is not registered. Stop sending messages to this token.")
    except PushServerError as exc:
        # Encountered some likely formatting/validation error.
        print(f"Push server error: {exc.message}, Errors: {exc.errors}, Response data: {exc.response_data}")
    except (ConnectionError, HTTPError) as exc:
        # Encountered some Connection or HTTP error - retry a few times in case it is transient.
        print(f"Connection or HTTP error: {exc}")
    except Exception as exc:
        print(f"An unexpected error occurred: {exc}")

if __name__ == "__main__":
    if not EXPO_ACCESS_TOKEN:
        print("Warning: EXPO_TOKEN environment variable not set. Push security might be enabled on your Expo account.")
    if EXPO_PUSH_TOKEN == 'ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]':
        print("Warning: EXPO_PUSH_TOKEN not set. Using a placeholder. Replace with an actual device token.")

    send_expo_push_message(
        token=EXPO_PUSH_TOKEN,
        title="Hello from Python!",
        body="This is a test notification from the Expo Server SDK.",
        data={"key": "value", "another": "data"}
    )

view raw JSON →