{ "cells": [ { "cell_type": "markdown", "id": "92475c69", "metadata": {}, "source": [ "# Python API Example - Access Regas Costs\n", "\n", "This guide is designed to provide an example of how to access the Spark API:\n", "- The path to your client credentials is the only input needed to run this script (just before Section 2)\n", "- This script has been designed to display the raw outputs of requests from the API, and then shows you how to format those outputs to enable easy reading and analysis\n", "\n", "__N.B. This guide is just for Access Regas Costs data. If you're looking for other API data products (such as contract prices, Freight routes or Netbacks), please refer to their according code example files.__ " ] }, { "cell_type": "markdown", "id": "fc7443de", "metadata": {}, "source": [ "### Have any questions?\n", "\n", "If you have any questions regarding our API, or need help accessing specific datasets, please contact us at:\n", "\n", "__data@sparkcommodities.com__\n", "\n", "or refer to our API website for more information about this endpoint: https://www.sparkcommodities.com/api/request/access.html" ] }, { "cell_type": "markdown", "id": "c5716130", "metadata": {}, "source": [ "## 1. Importing Data\n", "\n", "Here we define the functions that allow us to retrieve the valid credentials to access the Spark API.\n", "\n", "This section can remain unchanged for most Spark API users." ] }, { "cell_type": "code", "execution_count": 139, "id": "9aa7eca9", "metadata": {}, "outputs": [], "source": [ "# import libraries for callin the API\n", "import json\n", "import os\n", "import sys\n", "import pandas as pd\n", "from base64 import b64encode\n", "from urllib.parse import urljoin\n", "from pprint import pprint\n", "\n", "try:\n", " from urllib import request, parse\n", " from urllib.error import HTTPError\n", "except ImportError:\n", " raise RuntimeError(\"Python 3 required\")" ] }, { "cell_type": "code", "execution_count": 140, "id": "fe759439", "metadata": {}, "outputs": [], "source": [ "# defining query functions \n", "API_BASE_URL = \"https://api.sparkcommodities.com\"\n", "\n", "def retrieve_credentials(file_path=None):\n", " \"\"\"\n", " Find credentials either by reading the client_credentials file or reading\n", " environment variables\n", " \"\"\"\n", " if file_path is None:\n", " client_id = os.getenv(\"SPARK_CLIENT_ID\")\n", " client_secret = os.getenv(\"SPARK_CLIENT_SECRET\")\n", " if not client_id or not client_secret:\n", " raise RuntimeError(\n", " \"SPARK_CLIENT_ID and SPARK_CLIENT_SECRET environment vars required\"\n", " )\n", " else:\n", " # Parse the file\n", " if not os.path.isfile(file_path):\n", " raise RuntimeError(\"The file {} doesn't exist\".format(file_path))\n", "\n", " with open(file_path) as fp:\n", " lines = [l.replace(\"\\n\", \"\") for l in fp.readlines()]\n", "\n", " if lines[0] in (\"clientId,clientSecret\", \"client_id,client_secret\"):\n", " client_id, client_secret = lines[1].split(\",\")\n", " else:\n", " print(\"First line read: '{}'\".format(lines[0]))\n", " raise RuntimeError(\n", " \"The specified file {} doesn't look like to be a Spark API client \"\n", " \"credentials file\".format(file_path)\n", " )\n", "\n", " print(\">>>> Found credentials!\")\n", " print(\n", " \">>>> Client_id={}****, client_secret={}****\".format(\n", " client_id[:5], client_secret[:5]\n", " )\n", " )\n", "\n", " return client_id, client_secret\n", "\n", "\n", "def do_api_post_query(uri, body, headers):\n", " \"\"\"\n", " OAuth2 authentication requires a POST request with client credentials before accessing the API. \n", " This POST request will return an Access Token which will be used for the API GET request.\n", " \"\"\"\n", " url = urljoin(API_BASE_URL, uri)\n", "\n", " data = json.dumps(body).encode(\"utf-8\")\n", "\n", " # HTTP POST request\n", " req = request.Request(url, data=data, headers=headers)\n", " try:\n", " response = request.urlopen(req)\n", " except HTTPError as e:\n", " print(\"HTTP Error: \", e.code)\n", " print(e.read())\n", " sys.exit(1)\n", "\n", " resp_content = response.read()\n", "\n", " # The server must return HTTP 201. Raise an error if this is not the case\n", " assert response.status == 201, resp_content\n", "\n", " # The server returned a JSON response\n", " content = json.loads(resp_content)\n", "\n", " return content\n", "\n", "\n", "def do_api_get_query(uri, access_token, format='json'):\n", " \"\"\"\n", " After receiving an Access Token, we can request information from the API.\n", " \"\"\"\n", " url = urljoin(API_BASE_URL, uri)\n", " print(url)\n", "\n", " if format == 'json':\n", " headers = {\n", " \"Authorization\": \"Bearer {}\".format(access_token),\n", " \"Accept\": \"application/json\",\n", " }\n", " elif format == 'csv':\n", " headers = {\n", " \"Authorization\": \"Bearer {}\".format(access_token),\n", " \"Accept\": \"text/csv\"\n", " }\n", "\n", " # HTTP POST request\n", " req = request.Request(url, headers=headers)\n", " try:\n", " response = request.urlopen(req)\n", " except HTTPError as e:\n", " print(\"HTTP Error: \", e.code)\n", " print(e.read())\n", " sys.exit(1)\n", "\n", " resp_content = response.read()\n", " status = response.status\n", " print(status)\n", "\n", " # The server must return HTTP 201. Raise an error if this is not the case\n", " assert response.status == 200, resp_content\n", "\n", " # Storing response based on requested format\n", " if format == 'json':\n", " content = json.loads(resp_content)\n", " elif format == 'csv':\n", " content = resp_content\n", "\n", " return content\n", "\n", "\n", "\n", "def get_access_token(client_id, client_secret):\n", " \"\"\"\n", " Get a new access_token. Access tokens are the thing that applications use to make\n", " API requests. Access tokens must be kept confidential in storage.\n", "\n", " # Procedure:\n", "\n", " Do a POST query with `grantType` and `scopes` in the body. A basic authorization\n", " HTTP header is required. The \"Basic\" HTTP authentication scheme is defined in\n", " RFC 7617, which transmits credentials as `clientId:clientSecret` pairs, encoded\n", " using base64.\n", " \"\"\"\n", "\n", " # Note: for the sake of this example, we choose to use the Python urllib from the\n", " # standard lib. One should consider using https://requests.readthedocs.io/\n", "\n", " print(\"{}:{}\".format(client_id, client_secret))\n", " payload = \"{}:{}\".format(client_id, client_secret).encode()\n", " headers = {\n", " \"Authorization\": b64encode(payload).decode(),\n", " \"Accept\": \"application/json\",\n", " \"Content-Type\": \"application/json\",\n", " }\n", " body = {\n", " \"grantType\": \"clientCredentials\",\n", " }\n", "\n", " content = do_api_post_query(uri=\"/oauth/token/\", body=body, headers=headers)\n", "\n", "\n", " return content[\"accessToken\"]" ] }, { "cell_type": "markdown", "id": "a51e1ef0", "metadata": {}, "source": [ "## N.B. Credentials\n", "\n", "Here we call the above functions, and input the file path to our credentials.\n", "\n", "N.B. You must have downloaded your client credentials CSV file before proceeding. Please refer to the API documentation if you have not dowloaded them already. Instructions for downloading your credentials can be found here:\n", "\n", "https://api.sparkcommodities.com/redoc#section/Authentication/Create-an-Oauth2-Client\n" ] }, { "cell_type": "code", "execution_count": null, "id": "0817250f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ">>>> Found credentials!\n", ">>>> Client_id=01c23****, client_secret=80763****\n", "01c23590-ef6c-4a36-8237-c89c3f1a3b2a:80763560971790f4920a90a0dcb28698cd60734c71f6bfcbb71a53dd9cd27f198cbf1445faf2bd060c069e10b8a9dd73c9e5aedbc8a0722ed28f7e2246d43335eeb754d0a9aba437f84cf979a3b3ad546646eb1910429450f81ba94f938eed4be07cb253f7f2d55162e97877e2eec642fad34b51df9e833b483a62bd64662b1c\n" ] } ], "source": [ "# Insert file path to your client credentials here\n", "client_id, client_secret = retrieve_credentials(file_path=\"/tmp/client_credentials.csv\")\n", "\n", "# Authenticate:\n", "access_token = get_access_token(client_id, client_secret)" ] }, { "cell_type": "markdown", "id": "bdc1fb62", "metadata": {}, "source": [ "# 2. Reference Data\n", "\n", "Fetching the reference-data endpoint to obtain a list of available terminals & their corresponding UUIDs." ] }, { "cell_type": "code", "execution_count": 142, "id": "0f4ddd6e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "https://api.sparkcommodities.com/v1.0/lng/access/regas-costs/reference-data/\n", "200\n" ] }, { "data": { "text/html": [ "
| \n", " | TerminalUUID | \n", "TerminalName | \n", "
|---|---|---|
| 0 | \n", "00317185-978a-4df5-970c-2c28d3ab893c | \n", "Isle of Grain | \n", "
| 1 | \n", "00319041-261a-45f8-b4a0-826c4bbc5947 | \n", "Revithoussa | \n", "
| 2 | \n", "0031994e-f370-4927-ba88-a4e7a78c42db | \n", "Zeebrugge | \n", "
| 3 | \n", "0032d353-d0d8-4454-9b6c-a6c5db12e49d | \n", "Spain TVB | \n", "
| 4 | \n", "0032fecd-cec3-422b-939e-75aecc32f94c | \n", "Świnoujście | \n", "
| 5 | \n", "00332c69-88d1-4a7a-bcc7-0da39e850e90 | \n", "KRK | \n", "
| 6 | \n", "00338f3f-8875-435d-87a9-f83d9a5c5241 | \n", "Dunkerque | \n", "
| 7 | \n", "00355021-dc45-4aaa-8178-a6dc360c07b9 | \n", "OLT Toscana | \n", "
| 8 | \n", "003b1adb-f810-443c-a971-c2a6b28cb5dc | \n", "Fos Cavaou | \n", "
| 9 | \n", "003b1d25-f4bd-43bf-9cf6-9bd38216fe0f | \n", "Montoir | \n", "
| 10 | \n", "003bf9ab-2829-40a1-a83d-c32b764f21fd | \n", "South Hook | \n", "
| 11 | \n", "003e9c62-1047-45c4-a0e2-ebba5d73cf3b | \n", "Sines | \n", "
| 12 | \n", "003f577c-7058-4b50-9c94-c499c07ca080 | \n", "Gate | \n", "
| 13 | \n", "003f55df-08c7-4b6a-8597-fbe9e3f398f8 | \n", "Klaipeda | \n", "
| 14 | \n", "003d153e-282a-47a1-bb0c-b4fe0bc62d38 | \n", "Inkoo | \n", "
| 15 | \n", "00361ab8-f70d-4a08-8e45-e6eb5a0b8b2f | \n", "Le Havre | \n", "
| 16 | \n", "003660ee-567d-4d23-9e43-2891509b7bfb | \n", "Piombino | \n", "
| 17 | \n", "003497c6-ed32-412f-95ef-c3b1f962464e | \n", "Brunsbuttel | \n", "
| 18 | \n", "003b1d36-d72b-4331-888f-22b3f84c1cce | \n", "Wilhelmshaven 1 | \n", "
| 19 | \n", "0039f836-cd97-4965-b7a5-74c2cd307956 | \n", "Deutsche Ostsee Phase 1 | \n", "
| 20 | \n", "003e3e70-3626-4124-8ee9-d3ec39678e8c | \n", "Deutsche Ostsee | \n", "
| 21 | \n", "00378624-8afa-4cce-987e-e76cebe077ab | \n", "Wilhelmshaven 2 | \n", "
| 22 | \n", "0037d9e4-cf09-4f26-8934-f1e038e185ea | \n", "EemsEnergyTerminal | \n", "
| 23 | \n", "0038a35c-253f-44f5-a4e5-d5240d98039a | \n", "Adriatic | \n", "
| 24 | \n", "003b319e-b29e-4853-b4ee-85794d5bacba | \n", "Stade | \n", "
| 25 | \n", "0030d930-6574-4049-a739-327a16620429 | \n", "Ravenna | \n", "
| 26 | \n", "00352746-7b90-4f69-a995-6048f670c1b8 | \n", "Alexandroupolis | \n", "
| \n", " | ReleaseDate | \n", "DeliveryMonth | \n", "DeliveryMonthName | \n", "DeliveryMonthIndex | \n", "TerminalUUID | \n", "TerminalName | \n", "VesselSize | \n", "SlotBerth | \n", "SlotUnloadStorageRegas | \n", "SlotBerthUnloadStorageRegas | \n", "... | \n", "AdditionalStorage | \n", "AdditionalSendout | \n", "FuelGasLossesGasInKind | \n", "EntryCapacity | \n", "EntryVariable | \n", "Emissions | \n", "Power | \n", "TotalRegasCost | \n", "PortCost | \n", "TotalWithPortCost | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", "2026-01-14 | \n", "2026-02-01 | \n", "Feb26 | \n", "M+1 | \n", "0030d930-6574-4049-a739-327a16620429 | \n", "Ravenna | \n", "174000 | \n", "NaN | \n", "NaN | \n", "1.002 | \n", "... | \n", "NaN | \n", "NaN | \n", "0.167 | \n", "0.126 | \n", "NaN | \n", "0.103 | \n", "NaN | \n", "1.398 | \n", "0.061 | \n", "1.459 | \n", "
| 1 | \n", "2026-01-14 | \n", "2026-03-01 | \n", "Mar26 | \n", "M+2 | \n", "0030d930-6574-4049-a739-327a16620429 | \n", "Ravenna | \n", "174000 | \n", "NaN | \n", "NaN | \n", "1.002 | \n", "... | \n", "NaN | \n", "NaN | \n", "0.158 | \n", "0.126 | \n", "NaN | \n", "0.103 | \n", "NaN | \n", "1.389 | \n", "0.061 | \n", "1.450 | \n", "
| 2 | \n", "2026-01-14 | \n", "2026-04-01 | \n", "Apr26 | \n", "M+3 | \n", "0030d930-6574-4049-a739-327a16620429 | \n", "Ravenna | \n", "174000 | \n", "NaN | \n", "NaN | \n", "1.002 | \n", "... | \n", "NaN | \n", "NaN | \n", "0.148 | \n", "0.126 | \n", "NaN | \n", "0.103 | \n", "NaN | \n", "1.379 | \n", "0.061 | \n", "1.440 | \n", "
3 rows × 21 columns
\n", "| \n", " | ReleaseDate | \n", "DeliveryMonth | \n", "DeliveryMonthName | \n", "DeliveryMonthIndex | \n", "TerminalUUID | \n", "TerminalName | \n", "VesselSize | \n", "SlotBerth | \n", "SlotUnloadStorageRegas | \n", "SlotBerthUnloadStorageRegas | \n", "... | \n", "AdditionalStorage | \n", "AdditionalSendout | \n", "FuelGasLossesGasInKind | \n", "EntryCapacity | \n", "EntryVariable | \n", "Emissions | \n", "Power | \n", "TotalRegasCost | \n", "PortCost | \n", "TotalWithPortCost | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", "2026-01-02 | \n", "2026-02-01 | \n", "Feb26 | \n", "M+1 | \n", "0030d930-6574-4049-a739-327a16620429 | \n", "Ravenna | \n", "174000 | \n", "NaN | \n", "NaN | \n", "1.01 | \n", "... | \n", "NaN | \n", "NaN | \n", "0.152 | \n", "0.127 | \n", "NaN | \n", "0.104 | \n", "NaN | \n", "1.393 | \n", "0.062 | \n", "1.455 | \n", "
| 1 | \n", "2026-01-02 | \n", "2026-03-01 | \n", "Mar26 | \n", "M+2 | \n", "0030d930-6574-4049-a739-327a16620429 | \n", "Ravenna | \n", "174000 | \n", "NaN | \n", "NaN | \n", "1.01 | \n", "... | \n", "NaN | \n", "NaN | \n", "0.146 | \n", "0.127 | \n", "NaN | \n", "0.104 | \n", "NaN | \n", "1.387 | \n", "0.062 | \n", "1.449 | \n", "
| 2 | \n", "2026-01-02 | \n", "2026-04-01 | \n", "Apr26 | \n", "M+3 | \n", "0030d930-6574-4049-a739-327a16620429 | \n", "Ravenna | \n", "174000 | \n", "NaN | \n", "NaN | \n", "1.01 | \n", "... | \n", "NaN | \n", "NaN | \n", "0.147 | \n", "0.127 | \n", "NaN | \n", "0.104 | \n", "NaN | \n", "1.388 | \n", "0.062 | \n", "1.450 | \n", "
| 3 | \n", "2026-01-02 | \n", "2026-05-01 | \n", "May26 | \n", "M+4 | \n", "0030d930-6574-4049-a739-327a16620429 | \n", "Ravenna | \n", "174000 | \n", "NaN | \n", "NaN | \n", "1.01 | \n", "... | \n", "NaN | \n", "NaN | \n", "0.144 | \n", "0.127 | \n", "NaN | \n", "0.104 | \n", "NaN | \n", "1.385 | \n", "0.062 | \n", "1.447 | \n", "
| 4 | \n", "2026-01-02 | \n", "2026-06-01 | \n", "Jun26 | \n", "M+5 | \n", "0030d930-6574-4049-a739-327a16620429 | \n", "Ravenna | \n", "174000 | \n", "NaN | \n", "NaN | \n", "1.01 | \n", "... | \n", "NaN | \n", "NaN | \n", "0.144 | \n", "0.127 | \n", "NaN | \n", "0.104 | \n", "NaN | \n", "1.385 | \n", "0.062 | \n", "1.447 | \n", "
5 rows × 21 columns
\n", "| \n", " | ReleaseDate | \n", "DeliveryMonth | \n", "DeliveryMonthName | \n", "DeliveryMonthIndex | \n", "TerminalUUID | \n", "TerminalName | \n", "VesselSize | \n", "SlotBerth | \n", "SlotUnloadStorageRegas | \n", "SlotBerthUnloadStorageRegas | \n", "... | \n", "AdditionalStorage | \n", "AdditionalSendout | \n", "FuelGasLossesGasInKind | \n", "EntryCapacity | \n", "EntryVariable | \n", "Emissions | \n", "Power | \n", "TotalRegasCost | \n", "PortCost | \n", "TotalWithPortCost | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", "2026-01-02 | \n", "2026-02-01 | \n", "Feb26 | \n", "M+1 | \n", "003b1d25-f4bd-43bf-9cf6-9bd38216fe0f | \n", "Montoir | \n", "174000 | \n", "0.028 | \n", "0.229 | \n", "NaN | \n", "... | \n", "NaN | \n", "0.018 | \n", "0.049 | \n", "0.109 | \n", "NaN | \n", "0.035 | \n", "NaN | \n", "0.468 | \n", "0.054 | \n", "0.522 | \n", "
| 1 | \n", "2026-01-02 | \n", "2026-03-01 | \n", "Mar26 | \n", "M+2 | \n", "003b1d25-f4bd-43bf-9cf6-9bd38216fe0f | \n", "Montoir | \n", "174000 | \n", "0.028 | \n", "0.229 | \n", "NaN | \n", "... | \n", "NaN | \n", "0.018 | \n", "0.048 | \n", "0.109 | \n", "NaN | \n", "0.035 | \n", "NaN | \n", "0.467 | \n", "0.054 | \n", "0.521 | \n", "
| 2 | \n", "2026-01-02 | \n", "2026-04-01 | \n", "Apr26 | \n", "M+3 | \n", "003b1d25-f4bd-43bf-9cf6-9bd38216fe0f | \n", "Montoir | \n", "174000 | \n", "0.028 | \n", "0.229 | \n", "NaN | \n", "... | \n", "NaN | \n", "0.018 | \n", "0.047 | \n", "0.112 | \n", "NaN | \n", "0.035 | \n", "NaN | \n", "0.469 | \n", "0.054 | \n", "0.523 | \n", "
| 3 | \n", "2026-01-02 | \n", "2026-05-01 | \n", "May26 | \n", "M+4 | \n", "003b1d25-f4bd-43bf-9cf6-9bd38216fe0f | \n", "Montoir | \n", "174000 | \n", "0.028 | \n", "0.229 | \n", "NaN | \n", "... | \n", "NaN | \n", "0.018 | \n", "0.046 | \n", "0.112 | \n", "NaN | \n", "0.035 | \n", "NaN | \n", "0.468 | \n", "0.054 | \n", "0.522 | \n", "
| 4 | \n", "2026-01-02 | \n", "2026-06-01 | \n", "Jun26 | \n", "M+5 | \n", "003b1d25-f4bd-43bf-9cf6-9bd38216fe0f | \n", "Montoir | \n", "174000 | \n", "0.028 | \n", "0.229 | \n", "NaN | \n", "... | \n", "NaN | \n", "0.018 | \n", "0.046 | \n", "0.112 | \n", "NaN | \n", "0.035 | \n", "NaN | \n", "0.468 | \n", "0.054 | \n", "0.522 | \n", "
5 rows × 21 columns
\n", "