Skip to content

Python Code Examples

This guide demonstrates how to use the Spark API with Python. The example script shows you how to authenticate and fetch price releases.

Requirements

  • Python 3.7 or higher

Getting Started

1. Download the Example Script

Download our ready-to-use Python script: spark_price_releases.py

This script demonstrates how to:

  • Authenticate with the Spark API
  • Fetch available contracts
  • Retrieve the latest price releases for specific contracts

2. Run the Script

You can provide your credentials in two ways:

Option A: Using a Credentials File

shell
python spark_price_releases.py <path-to-credentials.csv>

Option B: Using Environment Variables

shell
export SPARK_CLIENT_ID=your_client_id_here
export SPARK_CLIENT_SECRET=your_client_secret_here
python spark_price_releases.py

Expected Output

When the script runs successfully, you'll see output similar to this:

text
>>>> Running Spark API Python sample...
>>>> Found credentials!
>>>> Client_id=166f3ee3-****, client_secret=2a14d****
>>>> Successfully fetched an access token eyJ0e****, valid 1799 seconds.

>>>> Contracts:
Spark25S Pacific 160 TFDE
Spark25F Pacific 160 TFDE
Spark25Fo Pacific 160 TFDE
...

>>>> Get latest price release for spark25s
release date = 2020-12-14
Spark Price is USD 186000 /day for period starting on 2020-12-29

>>>> Get latest price release for spark25f
release date = 2020-12-14
Spark Price is USD 187500 /day for period starting on 2020-01-01

>>>> Get latest price release for spark25fo
release date = 2020-12-14
Spark Price is USD 192250 /day for period starting on 2021-01-01
Spark Price is USD 128000 /day for period starting on 2021-02-01
...

>>>> Well done, you successfully ran your first queries!

Troubleshooting

Authentication errors?

  • Verify your client ID and secret are correct
  • Ensure your credentials haven't expired
  • Check that your subscription has API access

Need help? Contact us at support@sparkcommodities.com