The OpenElectricity Python SDK provides a simple interface to interact with the OpenElectricity API. It supports both synchronous and asynchronous operations.
pip install openelectricity
The SDK requires an API key for authentication. You can provide it in two ways:
- Environment variable:
export OPENELECTRICITY_API_KEY="your-api-key"
- Directly in code:
from openelectricity import OEClient
client = OEClient(api_key="your-api-key")
from openelectricity import OEClient
# Create a client
client = OEClient()
# Use context manager for automatic cleanup
with OEClient() as client:
# Get list of networks
networks = client.get_networks()
print(f"Found {len(networks)} networks")
# Access network information
for network in networks:
print(f"Network: {network.label} ({network.code})")
print(f"Regions: {[r.code for r in network.regions]}")
from openelectricity import AsyncOEClient
import asyncio
async def main():
async with AsyncOEClient() as client:
# Get list of networks
networks = await client.get_networks()
print(f"Found {len(networks)} networks")
# Access network information
for network in networks:
print(f"Network: {network.label} ({network.code})")
print(f"Regions: {[r.code for r in network.regions]}")
# Run the async code
asyncio.run(main())
# Synchronous
networks = client.get_networks()
for network in networks:
print(f"Network: {network.label} ({network.code})")
for region in network.regions:
print(f" Region: {region.code} (Timezone: {region.timezone})")
# Asynchronous
networks = await client.get_networks()
for network in networks:
print(f"Network: {network.label} ({network.code})")
for region in network.regions:
print(f" Region: {region.code} (Timezone: {region.timezone})")
The Network
object includes:
code
: Network codecountry
: Country codelabel
: Network label/nameregions
: List of regions in the networktimezone
: Network timezoneinterval_size
: Size of data intervals in minutes
The NetworkRegion
object includes:
code
: Region codetimezone
: Region timezone
The SDK provides custom exceptions for error handling:
OpenElectricityError
: Base exception classAPIError
: Raised when the API returns an error response
from openelectricity import OEClient, OpenElectricityError, APIError
try:
with OEClient() as client:
networks = client.get_networks()
except APIError as e:
print(f"API Error {e.status_code}: {e.detail}")
except OpenElectricityError as e:
print(f"SDK Error: {e}")
You can customize the client by providing a different base URL:
client = OEClient(base_url="https://api.staging.openelectricity.org.au/v4")