# Quick Start

Get up and running with OilPriceAPI in under 5 minutes.

# 🚀 Overview

This guide will help you:

  1. Get your API key
  2. Make your first API request
  3. Understand the response format
  4. Explore next steps

# Step 1: Get Your API Key

# Sign Up

  1. Visit oilpriceapi.com/signup (opens new window)
  2. Create your account (free tier available)
  3. Verify your email address

# Generate API Key

  1. Log into your dashboard (opens new window)
  2. Navigate to API Keys section
  3. Click Create New Key
  4. Give it a descriptive name (e.g., "Production App")
  5. Copy your key immediately - it won't be shown again!
💡 Tip: Create separate keys for development and production environments

# Step 2: Make Your First Request

Let's fetch the latest oil prices:

# Step 3: Understanding the Response

Here's what you'll receive:

{
  "status": "success",
  "data": {
    "prices": {
      "WTI_USD": {
        "value": 78.45,
        "currency": "USD",
        "unit": "barrel",
        "timestamp": "2025-07-18T10:30:00Z",
        "change_24h": 0.82,
        "change_percentage_24h": 1.05
      },
      "BRENT_CRUDE_USD": {
        "value": 82.31,
        "currency": "USD",
        "unit": "barrel",
        "timestamp": "2025-07-18T10:30:00Z",
        "change_24h": 0.95,
        "change_percentage_24h": 1.17
      }
    }
  },
  "meta": {
    "request_id": "req_abc123",
    "response_time_ms": 42
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

# Key Fields Explained

Field Description Example
value Current spot price 78.45
currency Price currency USD
unit Measurement unit barrel
timestamp Last update time 2025-07-18T10:30:00Z
change_24h 24-hour price change 0.82
change_percent_24h 24-hour percentage change 1.05

# Step 4: Common Use Cases

# Get Specific Commodities

# Only get WTI and Natural Gas prices
curl "https://api.oilpriceapi.com/v1/prices/latest?by_code=WTI_USD,NATURAL_GAS_USD" \
  -H "Authorization: Token YOUR_API_KEY"
1
2
3

# Get Prices in Different Currency

# Get WTI and Brent prices in EUR
curl "https://api.oilpriceapi.com/v1/prices/latest?by_code=WTI_USD,BRENT_CRUDE_USD&currency=EUR" \
  -H "Authorization: Token YOUR_API_KEY"
1
2
3

# Get Historical Data

# Get past 24 hours of WTI data
curl "https://api.oilpriceapi.com/v1/prices/past_day?by_code=WTI_USD" \
  -H "Authorization: Token YOUR_API_KEY"
1
2
3

# Step 5: Best Practices

# 1. Store API Keys Securely

Never hardcode API keys:

// ❌ Bad
const apiKey = 'opa_live_abc123';

// ✅ Good
const apiKey = process.env.OILPRICE_API_KEY;
1
2
3
4
5

# 2. Handle Errors Gracefully

try {
  const response = await fetch('https://api.oilpriceapi.com/v1/prices/latest?by_code=WTI_USD,BRENT_CRUDE_USD', {
    headers: { 'Authorization': `Token ${apiKey}` }
  });
  
  if (!response.ok) {
    throw new Error(`API error: ${response.status}`);
  }
  
  const data = await response.json();
  return data;
  
} catch (error) {
  console.error('Failed to fetch prices:', error);
  // Implement fallback logic
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 3. Implement Caching

API updates every 5 minutes, so cache responses:

import time
from functools import lru_cache

@lru_cache(maxsize=1)
def get_cached_prices(cache_key):
    response = requests.get(
        'https://api.oilpriceapi.com/v1/prices/latest?by_code=WTI_USD,BRENT_CRUDE_USD',
        headers={'Authorization': f'Token {API_KEY}'}
    )
    return response.json()

# Generate new cache key every 5 minutes
cache_key = int(time.time() // 300)
prices = get_cached_prices(cache_key)
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# Step 6: Using SDKs

For easier integration, use our official SDKs:

# Quick Examples

# Price Alert System

async function checkPriceThreshold() {
  const response = await fetch('https://api.oilpriceapi.com/v1/prices/latest?by_code=WTI_USD', {
    headers: { 'Authorization': `Token ${process.env.OILPRICE_API_KEY}` }
  });
  
  const data = await response.json();
  const wtiPrice = data.data.prices.WTI_USD.value;
  
  if (wtiPrice > 80) {
    sendAlert(`WTI above $80: Currently $${wtiPrice}`);
  }
}

// Check every 5 minutes
setInterval(checkPriceThreshold, 5 * 60 * 1000);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# Simple Dashboard

<!DOCTYPE html>
<html>
<head>
  <title>Oil Prices</title>
</head>
<body>
  <h1>Live Oil Prices</h1>
  <div id="prices">Loading...</div>
  
  <script>
    async function updatePrices() {
      // Note: In production, proxy through your backend
      const response = await fetch('/api/prices');
      const data = await response.json();
      
      document.getElementById('prices').innerHTML = `
        <p>WTI: $${data.data.prices.WTI_USD.value}</p>
        <p>Brent: $${data.data.prices.BRENT_CRUDE_USD.value}</p>
        <p>Last Updated: ${new Date(data.data.prices.WTI_USD.timestamp).toLocaleString()}</p>
      `;
    }
    
    updatePrices();
    setInterval(updatePrices, 60000); // Update every minute
  </script>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

# What's Next?

Now that you've made your first request, explore:

# Need Help?