Metadata-Version: 2.4
Name: apitally-serverless
Version: 0.2.1
Summary: Simple API monitoring & analytics for REST APIs running on serverless platforms (e.g. Cloudflare Workers).
Project-URL: Homepage, https://apitally.io
Project-URL: Documentation, https://docs.apitally.io
Project-URL: Repository, https://github.com/apitally/apitally-py-serverless
Author-email: Apitally <hello@apitally.io>
License: MIT License
License-File: LICENSE
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Web Environment
Classifier: Framework :: FastAPI
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Information Technology
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Internet
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Topic :: Internet :: WWW/HTTP :: WSGI :: Middleware
Classifier: Topic :: Software Development
Classifier: Topic :: System :: Monitoring
Classifier: Typing :: Typed
Requires-Python: <4.0,>=3.10
Requires-Dist: typing-extensions>=4.0.0; python_version < '3.11'
Provides-Extra: fastapi
Requires-Dist: fastapi>=0.116.1; extra == 'fastapi'
Requires-Dist: starlette<1.0.0,>=0.47.2; extra == 'fastapi'
Provides-Extra: starlette
Requires-Dist: starlette<1.0.0,>=0.47.2; extra == 'starlette'
Description-Content-Type: text/markdown

<p align="center">
  <a href="https://apitally.io" target="_blank">
    <picture>
      <source media="(prefers-color-scheme: dark)" srcset="https://assets.apitally.io/logos/logo-horizontal-new-dark.png">
      <source media="(prefers-color-scheme: light)" srcset="https://assets.apitally.io/logos/logo-horizontal-new-light.png">
      <img alt="Apitally logo" src="https://assets.apitally.io/logos/logo-horizontal-new-light.png" width="220">
    </picture>
  </a>
</p>
<p align="center"><b>API monitoring & analytics made simple</b></p>
<p align="center" style="color: #ccc;">Metrics, logs, and alerts for your serverless APIs — with just a few lines of code.</p>
<br>
<p>
<picture>
  <source media="(prefers-color-scheme: dark)" srcset="https://assets.apitally.io/screenshots/overview-dark.png">
  <source media="(prefers-color-scheme: light)" srcset="https://assets.apitally.io/screenshots/overview-light.png">
  <img alt="Apitally dashboard" src="https://assets.apitally.io/screenshots/overview-light.png">
</picture>
</p>
<br>

# Apitally SDK for Cloudflare Python Workers

[![Tests](https://github.com/apitally/apitally-py-serverless/actions/workflows/tests.yaml/badge.svg?event=push)](https://github.com/apitally/apitally-py-serverless/actions)
[![Codecov](https://codecov.io/gh/apitally/apitally-py-serverless/graph/badge.svg?token=q76jRwq7Hi)](https://codecov.io/gh/apitally/apitally-py-serverless)
[![PyPI](https://img.shields.io/pypi/v/apitally-serverless?logo=pypi&logoColor=white&color=%23006dad)](https://pypi.org/project/apitally-serverless/)

Apitally is a simple API monitoring and analytics tool that makes it easy to understand API usage, monitor performance, and troubleshoot issues.
Get started in minutes by just adding a few lines of code and a Logpush job.

Learn more about Apitally on our 🌎 [website](https://apitally.io) or check out
the 📚 [documentation](https://docs.apitally.io).

This SDK is for APIs running on [Cloudflare Python Workers](https://developers.cloudflare.com/workers/languages/python/) and relies on [Logpush](https://developers.cloudflare.com/workers/observability/logs/logpush/) to send data to Apitally.

## Key features

### API analytics

Track traffic, error and performance metrics for your API, each endpoint and
individual API consumers, allowing you to make informed, data-driven engineering
and product decisions.

### Request logs

Drill down from insights to individual API requests or use powerful search and filters to
find specific requests. View correlated application logs for a complete picture
of each request, making troubleshooting faster and easier.

### Error tracking

Understand which validation rules in your endpoints cause client errors. Capture
error details and stack traces for 500 error responses.

### API monitoring & alerts

Get notified immediately if something isn't right using custom alerts and synthetic
uptime checks. Alert notifications can be delivered via email, Slack and Microsoft Teams.

## Supported frameworks

| Framework                                         | Supported versions | Setup guide                                                              |
| ------------------------------------------------- | ------------------ | ------------------------------------------------------------------------ |
| [**FastAPI**](https://github.com/fastapi/fastapi) | `>=0.116.1`        | [Link](https://docs.apitally.io/setup-guides/fastapi-cloudflare-workers) |

Apitally also supports many other web frameworks in [Python](https://github.com/apitally/apitally-py), [JavaScript](https://github.com/apitally/apitally-js), [Go](https://github.com/apitally/apitally-go), [.NET](https://github.com/apitally/apitally-dotnet) and [Java](https://github.com/apitally/apitally-java) via our other SDKs.

## Getting started

If you don't have an Apitally account yet, first [sign up here](https://app.apitally.io/?signup).

### 1. Create app in Apitally

Create an app in the Apitally dashboard and select **FastAPI (Cloudflare Workers)** as your framework. You'll see detailed setup instructions, which also include your client ID.

### 2. Create Logpush job

Log in to the [Cloudflare dashboard](https://dash.cloudflare.com/) and navigate to _Analytics & Logs > Logpush_. Create a [Logpush](https://developers.cloudflare.com/workers/observability/logs/logpush/) job with the following settings:

| Setting                      | Value                                                                              |
| ---------------------------- | ---------------------------------------------------------------------------------- |
| Destination                  | HTTP destination                                                                   |
| HTTP endpoint                | `https://hub.apitally.io/v2/{client-id}/{env}/logpush`                             |
| Dataset                      | Workers trace events                                                               |
| If logs match...             | Filtered logs: EventType equals `fetch` and ScriptName equals `{your-worker-name}` |
| Send the following fields... | General: Event, EventTimestampMs, Logs                                             |

In the HTTP endpoint, replace `{client-id}` with your app's client ID and `{env}` with the environment (e.g. `prod` or `dev`). In the filter criteria, replace `{your-worker-name}` with the name of your Worker, as specified in your Wrangler config.

### 3. Configure Worker

Enable [Workers Logs](https://developers.cloudflare.com/workers/observability/logs/workers-logs/) and [Logpush](https://developers.cloudflare.com/workers/observability/logs/logpush/) in your `wrangler.toml` configuration file:

```toml
logpush = true

[observability]
enabled = true
head_sampling_rate = 1

[observability.logs]
invocation_logs = true
```

### 4. Add middleware

Install the SDK:

```bash
uv add apitally-serverless
```

Then add the Apitally middleware to your FastAPI application:

```python
from fastapi import FastAPI
from apitally_serverless.fastapi import ApitallyMiddleware

app = FastAPI()
app.add_middleware(
    ApitallyMiddleware,
    log_request_headers=True,
    log_request_body=True,
    log_response_body=True,
)
```

For further instructions, see our
[setup guide for FastAPI on Cloudflare Workers](https://docs.apitally.io/setup-guides/fastapi-cloudflare-workers).

See the [SDK reference](https://docs.apitally.io/sdk-reference/python-serverless) for all available configuration options, including how to mask sensitive data, customize request logging, and more.

## Getting help

If you need help please
[create a new discussion](https://github.com/orgs/apitally/discussions/categories/q-a)
on GitHub or email us at [support@apitally.io](mailto:support@apitally.io). We'll get back to you as soon as possible.

## License

This library is licensed under the terms of the [MIT license](LICENSE).
