Blog / How-to
How to get oil prices in Tableau
Save the prices as a CSV file, then open it in Tableau with Connect > Text file. One request to /v1/prices/past_year with by_code=WTI_USD, interval=1d and format=csv returns a year of daily WTI prices as CSV. Ask for one code per request and append the files; the script in step 2 does that for you.
There is no OilPriceAPI connector built into Tableau. These steps follow Method 3 (CSV file import) of the Tableau integration docs. Daily history needs an active 7-day trial or a paid plan; a Free plan key gets 402 UPGRADE_REQUIRED.
By OilPriceAPI · Published · Export script run against the live API on Nov 27, 2026
The workflow
Start a trial or paid plan and set OILPRICEAPI_KEY
GET /v1/prices/past_year, one code per request
by_code, interval=1d, format=csv
Append the rows into oil_prices.csv
Tableau: Connect > Text file > oil_prices.csv
Re-run the script, then refresh the data source
1. Check the request in a terminal
curl -s -o wti.csv \
-H "Authorization: Token YOUR_API_KEY" \
"https://api.oilpriceapi.com/v1/prices/past_year?by_code=WTI_USD&interval=1d&format=csv"Always pass by_code. The file starts with this header row:
price,formatted,currency,code,created_at,type,price_type,unit,source,as_of,collected_at,ingested_at,observed_at,synthetic,age_daysOn Nov 27, 2026 this request returned 261 daily rows for WTI_USD on one page. Putting two codes in one request gave 522 rows across two pages of 500, and a CSV response carries only one page. The page count is in the X-Total-Pages response header.
2. Export every code to one CSV
Save this as export_for_tableau.py, set OILPRICEAPI_KEY in your environment, and run it with Python 3. It uses only the standard library. Edit CODES to change the series.
import csv
import json
import os
import sys
import urllib.error
import urllib.request
API = "https://api.oilpriceapi.com/v1/prices"
KEY = os.environ["OILPRICEAPI_KEY"]
CODES = ["WTI_USD", "BRENT_CRUDE_USD"]
OUT = "oil_prices.csv"
def get_csv(code):
url = f"{API}/past_year?by_code={code}&interval=1d&format=csv"
req = urllib.request.Request(url, headers={"Authorization": "Token " + KEY})
try:
with urllib.request.urlopen(req, timeout=30) as resp:
if resp.headers.get("X-Total-Pages", "1") != "1":
sys.exit(f"{code}: more than one page; use a shorter window")
return list(csv.reader(resp.read().decode("utf-8").splitlines()))
except urllib.error.HTTPError as err:
body = json.loads(err.read() or b"{}")
detail = body.get("error") if isinstance(body.get("error"), dict) else body
name = detail.get("code") or detail.get("error_code", "")
message = detail.get("message") or body.get("error", "")
sys.exit(f"{code}: {err.code} {name} {message}")
header, rows = None, []
for code in CODES: # one code per request keeps each year on one page
table = get_csv(code)
header = header or table[0]
rows.extend(table[1:])
with open(OUT, "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(header)
writer.writerows(rows)
print(f"Wrote {len(rows)} rows for {len(CODES)} codes to {OUT}")We ran it against the live API on Nov 27, 2026: it wrote 522 rows, 261 per code. With a wrong key it stops with 401 UNAUTHORIZED and the API's message. A Free plan key gets 402 UPGRADE_REQUIRED, and the script stops with that code instead of writing an empty file.
3. Connect Tableau to the file
- In Tableau Desktop, under Connect, choose Text file.
- Select
oil_prices.csv. Tableau detects the column types. - Check that
created_atcame in as a date andpriceas a number.
| Column | Use it as | What it holds |
|---|---|---|
code | Dimension | Which series the row belongs to, e.g. WTI_USD |
created_at | Date | The UTC day the daily value covers |
price | Measure | The daily value, as a number |
unit / currency | Dimension | barrel and USD for the two crude codes |
4. Build the chart
Put DAY(created_at) on Columns, AVG(price) on Rows and code on Color for one line per series. These calculated fields add a spread and a moving average:
// WTI: the WTI price, null on other rows
IF [Code] = "WTI_USD" THEN [Price] END
// Brent: the Brent price, null on other rows
IF [Code] = "BRENT_CRUDE_USD" THEN [Price] END
// Brent-WTI spread, with DAY([Created At]) on Columns
SUM([Brent]) - SUM([WTI])
// 7-day moving average (table calculation along the date axis)
WINDOW_AVG(SUM([Price]), -6, 0)Timestamps are UTC. Each row is one code's daily value, so aggregate by day, not by hour.
5. Keep it current
Re-run the script on a schedule (cron, or Task Scheduler on Windows), then refresh the data source in Tableau. Each run costs one request per code: two codes once a day is about 60 requests a month. Keep the API key in the environment of the machine that runs the script, never inside a workbook.
The docs page also describes a Web Data Connector and a Tableau Prep script. Use the CSV route above until those samples are updated for the multi-code response shape.
Related guides
- Tableau integration (docs): the source of these steps.
- Power BI integration (docs): the same data in Power BI.
- Historical oil price data API guide: custom date ranges, plan depth and the 402.
- How to call OilPriceAPI in Postman: try the request before you script it.
Try it with your own key
New accounts get a 7-day trial with 10,000 requests and no credit card. After that, the Free plan is 50 requests/day. The Developer plan is $19/month for 10,000 requests/month.
Sources
- Live calls to /v1/prices/past_year with format=csv, Nov 27, 2026: one code per request, two codes in one request, and a bad key.
- docs.oilpriceapi.com/integrations/tableau, Method 3, read Nov 27, 2026.