Async Python client library for the Octopus Energy REST API
Project description
Octopy Energy API
An async Python client library for the Octopus Energy REST API
Features
- Async API Client - Full async/await support with
httpx - Comprehensive Data Models - Fully typed Pydantic models for all API responses
- Secure - Built-in API key authentication
- Type-Safe - Complete type hints for better IDE support
- Automatic Pagination - Seamlessly fetch all pages of data
Installation
pip install octopy-energy-api
Requirements
- An Octopus Energy account and API key (if using the Account and Consumption endpoints)
Configuration
Create a .env file in your project root:
OCTOPUS_API_KEY=sk_live_xxxxxxxxxxxxxxxxxxxxx
OCTOPUS_ACCOUNT_NUMBER=A-XXXXXXXX
Or set environment variables directly:
export OCTOPUS_API_KEY=sk_live_xxxxxxxxxxxxxxxxxxxxx
export OCTOPUS_ACCOUNT_NUMBER=A-XXXXXXXX
Environment Variables
| Variable | Required | Default | Description |
|---|---|---|---|
OCTOPUS_API_KEY |
Yes | - | Your Octopus Energy API key |
OCTOPUS_ACCOUNT_NUMBER |
Yes | - | Your account number (A-XXXXXXXX format) |
OCTOPUS_API_BASE_URL |
No | https://api.octopus.energy/v1 |
API base URL |
Quick Start
import asyncio
from octopy import Octopy
from octopy.config import get_settings
async def main():
# Load settings from environment variables or .env file
settings = get_settings()
# Use the client as an async context manager
async with Octopy(settings) as client:
# Get account information
account = await client.get_account()
print(f"Account Number: {account.number}")
# Get consumption data
property = account.properties[0]
meter_point = property.electricity_meter_points[0]
meter = meter_point.meters[0]
consumption = await client.get_consumption(
meter_point=meter_point.mpan,
serial_number=meter.serial_number,
fuel="electricity"
)
for interval in consumption.results[:5]:
print(f"{interval.interval_start}: {interval.consumption} kWh")
if __name__ == "__main__":
asyncio.run(main())
API Coverage
Account Management
get_account()- Get account details with properties and meter pointsget_grid_supply_point(postcode)- Get GSP region from postcode
Consumption Data
get_consumption()- Fetch electricity or gas consumption with filtering and grouping
Products & Tariffs
get_products()- List available products with filtersget_product(product_code)- Get detailed product information
Pricing
get_unit_rates()- Get electricity/gas unit rates with date filteringget_standing_charges()- Get daily standing charges for tariffs
All paginated endpoints support automatic pagination with auto_paginate=True (default).
Usage Examples
See the examples/ directory for complete working examples:
- fetch_account_info.py - Display account, properties, and meters
- fetch_consumption.py - Fetch consumption data with statistics
- compare_tariffs.py - Compare unit rates and standing charges across products
Data Models
All API responses are returned as fully typed Pydantic models:
Account- Account information with propertiesProperty- Property with address and meter pointsElectricityMeterPoint/GasMeterPoint- Meter point detailsMeter- Physical meter with serial numberAgreement- Tariff agreement with validity datesConsumptionInterval- Energy consumption for a time periodProduct/ProductDetail- Energy product informationUnitRate- Unit rate pricingStandingCharge- Daily standing chargeRegion- Grid Supply Point (GSP) region enum
Development
Setup
Clone the repository and install dependencies:
git clone https://github.com/imspury/octopy-energy-api.git
cd octopy-energy-api
pip install -e ".[dev]"
Testing
This project uses pytest for testing with comprehensive test coverage.
Run Tests
# Run all tests
pytest
# Run with coverage report
pytest --cov=src/octopy --cov-report=term-missing
# Run with verbose output
pytest -v
# Run specific test file
pytest tests/test_client.py
# Run specific test
pytest tests/test_client.py::test_get_account
Test Structure
The test suite includes:
- Unit tests for all client methods and API endpoints
- HTTP mocking with
respxfor isolated testing (no real API calls) - Async test support with
pytest-asyncio - Comprehensive coverage of:
- Account management endpoints
- Consumption data with pagination and filtering
- Product and pricing endpoints
- Exception handling and error cases
- Configuration management
- Data model validation
Code Quality
# Run linting
ruff check src/ tests/ examples/
# Auto-fix linting issues
ruff check --fix src/ tests/ examples/
# Format code
ruff format src/ tests/ examples/
# Type checking
mypy src/octopy tests
License
This project is licensed under the MIT License - see LICENSE for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file octopy_energy_api-0.2.0.tar.gz.
File metadata
- Download URL: octopy_energy_api-0.2.0.tar.gz
- Upload date:
- Size: 25.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
99a5ecbd29d2d76d3ce213de35fd7bb66144ae5dba8b47cc0de1c7599708e935
|
|
| MD5 |
1e269708e77ef7a871d5b9fc623dd11a
|
|
| BLAKE2b-256 |
e6c53dcd4333e7b1b6a064c9eb6e7e4109ddd5588f644ac4febeb69c3bb3dcc6
|
File details
Details for the file octopy_energy_api-0.2.0-py3-none-any.whl.
File metadata
- Download URL: octopy_energy_api-0.2.0-py3-none-any.whl
- Upload date:
- Size: 12.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
885387d2ab7a4b66d4592f410d417e11be7f3683a7403dbb5798ee8e40a58897
|
|
| MD5 |
41dfb4c226aa7519977cbf1d12f35c52
|
|
| BLAKE2b-256 |
4b794b7c7af361996314494a5a463454bbbb3859cdd5e25b46551b64917ef5eb
|