Developer guide

OilPriceAPI with C# / .NET

Start with one authenticated Brent request, verify the returned source timestamp, then build outward from the canonical quickstart.

No official C#/.NET SDK or OilPriceAPI NuGet package is currently published

This guide uses the built-in HttpClient and cURL to call the OilPriceAPI REST API directly.

Runtime

.NET 6+ example

Environment

OILPRICEAPI_KEY

Authentication

Authorization: Token YOUR_API_KEY

First request

/v1/prices/latest?by_code=BRENT_CRUDE_USD

First request

Prove the API path first

Store the key as OILPRICEAPI_KEY and run the request from a trusted server process. Do not expose the key in browser bundles, logs, notebooks, or shared documents.

No test-key prefix

OilPriceAPI does not issue special test keys. Use the keyless demo endpoint or fixtures for tests; keep customer keys out of CI.

csharp

.NET 6+ example
using System.Net.Http.Headers;
using System.Text.Json;

var apiKey = Environment.GetEnvironmentVariable("OILPRICEAPI_KEY")
    ?? throw new InvalidOperationException("OILPRICEAPI_KEY is required");

using var client = new HttpClient { Timeout = TimeSpan.FromSeconds(10) };
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", $"Token {apiKey}");

using var response = await client.GetAsync("https://api.oilpriceapi.com/v1/prices/latest?by_code=BRENT_CRUDE_USD");
var json = await response.Content.ReadAsStringAsync();
response.EnsureSuccessStatusCode();

using var payload = JsonDocument.Parse(json);
Console.WriteLine(payload.RootElement.GetProperty("data"));

This example uses the built-in HttpClient and System.Text.Json.

Universal curl

Compare your client with the contract

curl "https://api.oilpriceapi.com/v1/prices/latest?by_code=BRENT_CRUDE_USD" \
  -H "Authorization: Token YOUR_API_KEY"

Expected shape

Read the timestamp from the response

{
  "status": "success",
  "data": {
    "code": "BRENT_CRUDE_USD",
    "price": 78.41,
    "currency": "USD",
    "created_at": "2026-03-09T12:00:00.000Z",
    "type": "spot_price"
  }
}

Keyless first touch

Use demo data before signup

The demo route is the supported no-key path. It is rate limited and intentionally narrower than authenticated, account-entitled access.

curl "https://api.oilpriceapi.com/v1/demo/prices"
Open demo response

Recover without guessing

ResultNext action
401Check that the header is exactly Authorization: Token YOUR_API_KEY.
402 / 403The dataset may require a plan or account entitlement. Check pricing or contact support.
429Read the rate-limit response and retry after the indicated reset; do not loop immediately.
Timeout / 5xxUse a bounded timeout and exponential backoff. Keep the last valid source timestamp visible.

Supported path

Use the REST API directly

Continue with the built-in language client above or the universal cURL request. Supported packages are available for Python, Node.js, Go, and PHP.