Authentication
You'll need to authenticate your requests to access any of the endpoints in the FuelPrice API. In this guide, we'll look at how authentication works.
Requests with bearer token
The recommended way to authenticate with the FuelPrice API is by using OAuth2. When establishing a connection using OAuth2, you will need your access token — you will find it in the FuelPrice dashboard under API settings.
In the below examples, replace the {token} placeholder with your own access token.
cURL
Example request with bearer token
curl https://api.fuelprice.io/v1/ \
-H "Authorization: Bearer {token}"
JavaScript & Node
JavaScript & Node request with bearer token
const url = `https://api.fuelprice.io/v1/`;
const response = await fetch(url, {
headers: {
"Authorization": `Bearer {token}`
}
});
Python
Python request with bearer token
import urllib.request
import json
url = "https://api.fuelprice.io/v1/"
token = "{token}"
request = urllib.request.Request(url)
request.add_header("Authorization", f"Bearer {token}")
with urllib.request.urlopen(request) as response:
data = json.loads(response.read().decode())
print(data)
PHP
PHP request with bearer token
<?php
$url = "https://api.fuelprice.io/v1/";
$token = "{token}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer " . $token
]);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
print_r($data);
?>
Ruby
Ruby request with bearer token
require 'net/http'
require 'json'
url = URI("https://api.fuelprice.io/v1/")
token = "{token}"
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Get.new(url)
request["Authorization"] = "Bearer #{token}"
response = http.request(request)
data = JSON.parse(response.body)
puts data
Go
Go request with bearer token
package main
import (
"fmt"
"io"
"net/http"
"encoding/json"
)
func main() {
url := "https://api.fuelprice.io/v1/"
token := "{token}"
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", "Bearer " + token)
client := &http.Client{}
resp, _ := client.Do(req)
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var data interface{}
json.Unmarshal(body, &data)
fmt.Println(data)
}
Java
Java request with bearer token
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.URI;
public class FuelPriceAPI {
public static void main(String[] args) throws Exception {
String token = "{token}";
String url = "https://api.fuelprice.io/v1/";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Authorization", "Bearer " + token)
.GET()
.build();
HttpResponse<String> response = client.send(request,
HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}
Always keep your token safe and reset it if you suspect it has been compromised.
