Skip to main content

Upstash Python QStash SDK

QStash is an HTTP based messaging and scheduling solution for serverless and edge runtimes.

QStash Documentation

Install

pip install qstash

Usage

You can get your QStash token from the Upstash Console.

Publish a JSON message

from qstash import QStash

client = QStash("<QSTASH_TOKEN>")

res = client.message.publish_json(
    url="https://example.com",
    body={"hello": "world"},
    headers={
        "test-header": "test-value",
    },
)

print(res.message_id)

Create a scheduled message

from qstash import QStash

client = QStash("<QSTASH_TOKEN>")

schedule_id = client.schedule.create(
    destination="https://example.com",
    cron="*/5 * * * *",
)

print(schedule_id)

Receiving messages

from qstash import Receiver

# Keys available from the QStash console
receiver = Receiver(
    current_signing_key="CURRENT_SIGNING_KEY",
    next_signing_key="NEXT_SIGNING_KEY",
)

# ... in your request handler

signature, body = req.headers["Upstash-Signature"], req.body

receiver.verify(
    body=body,
    signature=signature,
    url="https://example.com",  # Optional
)

Publish a JSON message to LLM Using Custom Providers

from qstash import QStash
from qstash.chat import openai

client = QStash("<QSTASH_TOKEN>")

res = client.message.publish_json(
    api={"name": "llm", "provider": openai("<OPENAI_API_KEY>")},
    body={
        "model": "gpt-3.5-turbo",
        "messages": [
            {
                "role": "user",
                "content": "What is the capital of Turkey?",
            }
        ],
    },
    callback="https://example-cb.com",
)

print(res.message_id)

Additional configuration

from qstash import QStash

# Create a client with a custom retry configuration. This is
# for sending messages to QStash, not for sending messages to
# your endpoints.
# The default configuration is:
# {
#   "retries": 5,
#   "backoff": lambda retry_count: math.exp(retry_count) * 50,
# }
client = QStash(
    token="<QSTASH_TOKEN>",
    retry={
        "retries": 1,
        "backoff": lambda retry_count: (2 ** retry_count) * 20,
    },
)

# Publish to URL
client.message.publish_json(
    url="https://example.com",
    body={"key": "value"},
    # Retry sending message to API 3 times
    # https://upstash.com/docs/qstash/features/retry
    retries=3,
    # Schedule message to be sent 4 seconds from now
    delay="4s",
    # When message is sent, send a request to this URL
    # https://upstash.com/docs/qstash/features/callbacks
    callback="https://example.com/callback",
    # When message fails to send, send a request to this URL
    failure_callback="https://example.com/failure_callback",
    # Headers to forward to the endpoint
    headers={
        "test-header": "test-value",
    },
    # Enable content-based deduplication
    # https://upstash.com/docs/qstash/features/deduplication#content-based-deduplication
    content_based_deduplication=True,
)

Additional methods are available for managing url groups, schedules, and messages. See the examples folder for more.

Development

  1. Clone the repository
  2. Install Poetry
  3. Install dependencies with poetry install
  4. Create a .env file with cp .env.example .env and fill in the QSTASH_TOKEN
  5. Run tests with poetry run pytest
  6. Format with poetry run ruff format .

Metadata

Release files for qstash 3.5.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 qstash 3.5.0
File Size Uploaded Provenance Actions
qstash-3.5.0.tar.gz 30.5 kB Without provenance Details for qstash-3.5.0.tar.gz

Built distribution (wheel)

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

Total release size: 72.6 kB

Release files / qstash-3.5.0.tar.gz

Download URL qstash-3.5.0.tar.gz
Size 30.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ff991021c6399eb5517098632d84afcb677346b8395705a78624627dfd9fe729
BLAKE2b-256 checksum
How to use checksums
83d45d8b32ef8769651667ce25db7afbe2afe5aa05ee8ed231b61d792ccda453
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.4 CPython/3.8.18 Linux/7.0.0-1017-azure

Release files / qstash-3.5.0-py3-none-any.whl

Download URL qstash-3.5.0-py3-none-any.whl
Size 42.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
112d90d993e9834a81b1fbe8a0bd35fb6fc83dcbc04331ecabfd9cefa8314076
BLAKE2b-256 checksum
How to use checksums
1bb66304e66cf1b1ed0e08eea140c0159acd3a1433b5905a3814a31abb5d59c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.4 CPython/3.8.18 Linux/7.0.0-1017-azure

Security for qstash 3.5.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.

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