Python code samples demonstrating how to integrate with Courier's notification platform.
- Python 3.7 or higher - Required to run Python samples
- pip - Python package manager (usually included with Python)
Check if Python 3.7+ is installed:
python3 --versionIf the command is not found or shows a version below 3.7, install Python 3.7 or higher:
- macOS (Homebrew):
brew install python3 - Linux (apt):
sudo apt-get install python3 - Windows: Download from python.org
Get up and running in 3 steps:
-
Set up the virtual environment and install dependencies:
cd server/python python3 -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate pip install -r requirements.txt
-
Set up environment variables:
Create a
.envfile in theserver/directory (shared across all language examples). You have two options:Option A: Using the interactive script (Recommended)
# From the project root, run set-env.sh for the variables you need bash scripts/set-env.sh --dir server api_key generate_jwt_user_idThe script will prompt you for each variable interactively. If
.env.exampleexists, it will be used as a starting point.Option B: Copy from .env.example and edit manually
cd server cp .env.example .env # Then edit .env with your actual values
The
.envfile should contain variables like:COURIER_API_KEY=your_api_key_here COURIER_GENERATE_JWT_USER_ID=your_user_id COURIER_SEND_TEMPLATE_TO_EMAIL_EMAIL=test@example.com COURIER_SEND_TEMPLATE_TO_EMAIL_TEMPLATE_ID=your_template_id # ... add other required variables as needed -
Run with VS Code Debugger:
- Open the Run and Debug panel (⇧⌘D or Ctrl+Shift+D)
- Select a Python configuration from the dropdown (e.g., "Python: Generate JWT")
- Press F5 or click the play button
The launch configurations will automatically:
- Set up environment variables (via
scripts/set-env.sh) - Activate the virtual environment
- Run the selected script
If you prefer to run scripts from the command line:
-
Navigate to the Python directory:
cd server/python -
Activate the virtual environment:
# On macOS/Linux: source venv/bin/activate # On Windows: venv\Scripts\activate
You should see
(venv)in your terminal prompt when activated. -
Run a sample:
python generate-jwt.py
Note: Always activate the virtual environment before running scripts.
All 11 Python samples are available (listed alphabetically):
create-list.py- Create or update a notification listgenerate-jwt.py- Generate JWT tokens for user authenticationget-user-profile.py- Retrieve a user profilesend-template-to-audience.py- Send notifications to an audiencesend-template-to-email.py- Send notifications to an email addresssend-template-to-list.py- Send notifications to a listsend-template-to-tenant.py- Send notifications to a tenantsend-template-to-user-id.py- Send notifications to a user IDsubscribe-user-to-list.py- Subscribe a user to a listunsubscribe-user-from-list.py- Unsubscribe a user from a listupsert-user.py- Create or update a user profile
The Python samples require:
courier- Official Courier Python SDKpython-dotenv- For loading environment variables from.envfiles
Install with:
cd server/python
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txtFor complete reference documentation, see:
To quickly validate all Python samples (syntax and structure):
cd tests
./quick_test.shThis will check:
- ✅ Python syntax validity
- ✅ Code structure and patterns
⚠️ Dependencies (will show as missing if not installed)
To test all samples with dependencies installed in a virtual environment:
cd tests
./test_with_deps.shThis script will:
- Create a Python virtual environment (if it doesn't exist)
- Install required dependencies (
courierandpython-dotenv) - Run comprehensive tests on all samples
All test scripts are located in the tests/ directory:
tests/test_all.py- Python test script that validates all samplestests/quick_test.sh- Quick validation without dependenciestests/test_with_deps.sh- Full test with virtual environment setup