Skip to main content
Python SDK for Oil Prices

Python Oil Price API

Get Oil Prices in Python with 3 Lines of Code

Simple Python library for real-time oil prices. Works with pandas, numpy, and your favorite data tools.

Quick Start2 min setup
pip install oilpriceapi

from oilpriceapi import OilPriceAPI

client = OilPriceAPI(api_key="your_free_key")
prices = client.get_latest_prices()

print(f"WTI: ${prices['WTI_USD']}/barrel")
print(f"Brent: ${prices['BRENT_USD']}/barrel")

# Output:
# WTI: $63.53/barrel
# Brent: $67.91/barrel

Installation & Setup

1. Install via pip

pip install oilpriceapi

Requirements: Python 3.7+, requests library (auto-installed)

2. Get Your API Key

Sign up for a free account to get your API key. No credit card required.

3. Make Your First Call

from oilpriceapi import OilPriceAPI

# Initialize client
client = OilPriceAPI(api_key="YOUR_API_KEY")

# Get latest prices
latest = client.get_latest_prices()

# Get historical data
historical = client.get_historical(
    commodity="WTI_USD",
    start_date="2024-01-01",
    end_date="2024-12-31"
)

Python Code Examples

Pandas DataFrame Integration

import pandas as pd
from oilpriceapi import OilPriceAPI

client = OilPriceAPI(api_key="KEY")

# Get data as DataFrame
df = client.get_dataframe(
    commodities=["WTI_USD", "BRENT_USD"],
    start_date="2024-01-01"
)

# Analyze with pandas
df['spread'] = df['BRENT_USD'] - df['WTI_USD']
monthly_avg = df.resample('M').mean()

WebSocket Streaming

from oilpriceapi import OilPriceStream

stream = OilPriceStream(api_key="KEY")

def on_price_update(data):
    print(f"New price: {data}")

# Stream real-time prices
stream.subscribe(
    commodities=["WTI_USD"],
    callback=on_price_update
)

stream.connect()

Price Alert System

from oilpriceapi import OilPriceAPI

client = OilPriceAPI(api_key="KEY")

# Set price alerts
client.create_alert(
    commodity="WTI_USD",
    condition="above",
    threshold=70.00,
    webhook_url="https://your-app.com"
)

# Monitor positions
if client.get_price("WTI_USD") > 70:
    send_notification("WTI above $70!")

Statistical Analysis

import numpy as np
from oilpriceapi import OilPriceAPI

client = OilPriceAPI(api_key="KEY")

# Get 30-day historical
data = client.get_historical(
    "WTI_USD", days=30
)

prices = [d['price'] for d in data]

# Calculate metrics
volatility = np.std(prices)
mean_price = np.mean(prices)
price_range = max(prices) - min(prices)

Python SDK Features

Core Features

  • Async/await support for high performance
  • Automatic retry with exponential backoff
  • Built-in caching to reduce API calls
  • Type hints for better IDE support
  • Comprehensive error handling

Data Science Ready

  • Native pandas DataFrame support
  • NumPy array conversion
  • Jupyter notebook compatible
  • Matplotlib/Plotly integration
  • Time series analysis utilities

Built for Python Developers

Quantitative Trading

Build trading algorithms with real-time oil price data. Backtest strategies using historical data.

Data Analysis

Analyze energy markets with pandas and NumPy. Create visualizations with matplotlib.

Machine Learning

Train models to predict oil prices. Use scikit-learn, TensorFlow, or PyTorch with our data.

Django/Flask Apps

Build web applications with oil price data. Perfect for energy dashboards and analytics.

Automation Scripts

Automate reporting and alerts. Schedule cron jobs to monitor price movements.

Research & Academia

Academic research on energy markets. Economic modeling with real commodity data.

Start Using Oil Prices in Python Today

Join thousands of Python developers using our API

pip install oilpriceapi