An open API service indexing awesome lists of open source software.

https://github.com/android-sms-gateway/example-webhooks-fastapi

๐Ÿ” Example FastAPI webhook processor - demonstrates registration, HMAC validation, and payload handling.
https://github.com/android-sms-gateway/example-webhooks-fastapi

api-integration example fastapi python sms sms-gate sms-gateway webhook

Last synced: 6 months ago
JSON representation

๐Ÿ” Example FastAPI webhook processor - demonstrates registration, HMAC validation, and payload handling.

Awesome Lists containing this project

README

          

# ๐Ÿ“ฑ Example SMS Webhook Processor (FastAPI)

[![Example](https://img.shields.io/badge/Type-Example%20Project-orange.svg)](https://github.com/yourusername/sms-webhook-processor)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
[![Python](https://img.shields.io/badge/Python-3.9%2B-brightgreen.svg)](https://www.python.org/)
[![FastAPI](https://img.shields.io/badge/Framework-FastAPI-%2300C7B7.svg)](https://fastapi.tiangolo.com/)

> **โš ๏ธ Example Project Notice**
> Not intended for production use without proper security review and modifications.

## ๐Ÿ“‹ Table of Contents
- [๐Ÿ“ฑ Example SMS Webhook Processor (FastAPI)](#-example-sms-webhook-processor-fastapi)
- [๐Ÿ“‹ Table of Contents](#-table-of-contents)
- [โœจ About The Project](#-about-the-project)
- [๐Ÿ› ๏ธ Built With](#๏ธ-built-with)
- [โš ๏ธ Important Notes](#๏ธ-important-notes)
- [๐Ÿš€ Getting Started](#-getting-started)
- [๐Ÿ“ฆ Prerequisites](#-prerequisites)
- [โšก Installation](#-installation)
- [โš™๏ธ Configuration](#๏ธ-configuration)
- [๐Ÿ–ฅ๏ธ Usage](#๏ธ-usage)
- [๐Ÿ“š API Reference](#-api-reference)
- [`POST /webhook/sms-received`](#post-webhooksms-received)
- [๐Ÿค Contributing](#-contributing)
- [๐Ÿ“œ License](#-license)

## โœจ About The Project

**Example Project Features**:
- ๐Ÿงฉ Demonstrates webhook registration/deregistration lifecycle
- ๐Ÿ” Example HMAC signature validation implementation
- ๐Ÿ“ Sample payload validation using Pydantic models

### ๐Ÿ› ๏ธ Built With

- ๐Ÿš€ [FastAPI](https://fastapi.tiangolo.com/) - Modern Python web framework
- โœ”๏ธ [Pydantic](https://pydantic.dev/) - Data validation and settings management
- ๐ŸŒ [HTTPX](https://www.python-httpx.org/) - Async HTTP client
- โšก [UVicorn](https://www.uvicorn.org/) - ASGI server

### โš ๏ธ Important Notes

**This example intentionally omits**:

- Production-grade error handling
- Rate limiting
- Persistent storage integration
- Advanced security features

**Recommended for**:

- ๐Ÿงช Testing SMS Gate webhook integration
- ๐ŸŽ“ Learning FastAPI webhook implementations

## ๐Ÿš€ Getting Started

### ๐Ÿ“ฆ Prerequisites

- Python 3.9+ (development environment)
- Valid SSL certificate ([project's CA](https://docs.sms-gate.app/services/ca/) available) or reverse proxy (like [ngrok](https://ngrok.com/))
- SMS Gate credentials

### โšก Installation

1. Clone the example repository:
```bash
git clone https://github.com/android-sms-gateway/example-webhooks-fastapi.git
cd example-webhooks-fastapi
```

2. Install development dependencies:
```bash
pip install -r requirements.txt
# or
pipenv install
```

3. Create example environment file:
```bash
cp .env.example .env
```

## โš™๏ธ Configuration

**Example `.env` configuration**:
```ini
# ๐Ÿ”‘ Example SMS Gate API Credentials
SMS_GATE_API_URL="https://api.sms-gate.app/3rdparty/v1" # API root endpoint (optional)
SMS_GATE_API_USERNAME="test_user" # API username
SMS_GATE_API_PASSWORD="test_password" # API password

# ๐Ÿ”’ Example Webhook Security
WEBHOOK_SECRET="your_test_secret_here" # signing key (optional)
WEBHOOK_URL="https://localhost:8443/webhook/sms-received" # current server endpoint

# ๐Ÿ›ก๏ธ SSL Configuration
SSL_CERT_PATH="./certs/server.crt" # SSL certificate (optional)
SSL_KEY_PATH="./certs/server.key" # SSL private key (optional)
```

## ๐Ÿ–ฅ๏ธ Usage

**Run the example server**:
```bash
python main.py
```

**Expected output**:
```plaintext
INFO: Started server process [42516]
INFO: Waiting for application startup.
Registered webhook with ID: mIv93KBZgaFNGrhl_ivk9
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8080 (Press CTRL+C to quit)
Received SMS:
SIM: 1
From: 6505551212
Message: Android is always a sweet treat!
Received at: 2025-04-15 12:28:01+07:00
INFO: 169.150.246.92:0 - "POST /webhook/sms-received HTTP/1.1" 200 OK
^CINFO: Shutting down
INFO: Waiting for application shutdown.
Unregistered webhook ID: mIv93KBZgaFNGrhl_ivk9
INFO: Application shutdown complete.
INFO: Finished server process [42516]
```

## ๐Ÿ“š API Reference

### `POST /webhook/sms-received`

**Example Request**:
```bash
curl -X POST https://localhost:8443/webhook/sms-received \
-H "X-Signature: abc123..." \
-H "X-Timestamp: 1690123456" \
-d @sample_payload.json
```

**Example Response**:
```json
{
"status": "ok"
}
```

## ๐Ÿค Contributing

This example project welcomes contributions to:

- Improve documentation
- Demonstrate additional features
- Enhance example security implementations

## ๐Ÿ“œ License

This example code is released under [Apache License 2.0](LICENSE).