SDKs overview

Energi, a Danish data service, provides access to energy-related datasets through a RESTful API. To facilitate developer integration, Energi offers official Software Development Kits (SDKs) for several popular programming languages. These SDKs abstract the underlying HTTP requests and JSON parsing, allowing developers to interact with the Energi API using native language constructs. The primary goal of these SDKs is to reduce the boilerplate code required for API interactions, thereby accelerating development cycles for applications that integrate with Energi's electricity spot prices, gas prices, power grid data, and consumption data.

The official SDKs are designed to maintain consistency with the Energi API documentation, ensuring that developers can easily map API endpoints and data structures to SDK methods and objects. This approach aligns with industry best practices for API client libraries, which aim to provide a more idiomatic developer experience compared to direct HTTP client usage, as detailed by general API design principles for SDKs and libraries.

Official SDKs by language

Energi maintains official SDKs for four programming languages, ensuring comprehensive support for its developer community. These SDKs are actively developed and provide stable interfaces for accessing the various data streams offered by the Energi API. Each SDK is tailored to the conventions and best practices of its respective language, offering a familiar environment for developers.

Language Package Name Install Command Maturity
Python energi-python pip install energi-python Stable
JavaScript @energi/js-sdk npm install @energi/js-sdk Stable
PHP energi/php-sdk composer require energi/php-sdk Stable
Ruby energi-ruby gem install energi-ruby Stable

These SDKs encapsulate the necessary authentication mechanisms and error handling, providing a streamlined experience. Developers can refer to the Energi API documentation for detailed usage examples and method references for each SDK.

Installation

Installing Energi's official SDKs typically involves using the standard package manager for each respective language. Below are the installation instructions for Python, JavaScript, PHP, and Ruby. An active internet connection is required to download the packages from their respective repositories.

Python

The Python SDK can be installed using pip, Python's package installer. It is recommended to use a virtual environment to manage dependencies.

python -m venv .venv
source .venv/bin/activate  # On Windows, use `.venv\Scripts\activate`
pip install energi-python

JavaScript

For JavaScript projects, the SDK is available via npm (Node Package Manager). Ensure Node.js and npm are installed on your system.

npm install @energi/js-sdk

Alternatively, if you prefer Yarn:

yarn add @energi/js-sdk

PHP

The PHP SDK uses Composer for dependency management. If you don't have Composer installed, follow the Composer installation guide.

composer require energi/php-sdk

Ruby

The Ruby SDK is distributed as a Gem. You can install it using the gem command.

gem install energi-ruby

Quickstart example

This section provides a quickstart example using the Python SDK to retrieve current electricity spot prices. This example demonstrates basic initialization and a common API call. For more detailed examples and advanced features, consult the Energi API documentation specific to each SDK.

Python Quickstart

First, ensure you have installed the energi-python package as described in the installation section. You will also need an API key, which can be obtained from the Energi platform. The free API key offers limited requests and 24-hour historical data, suitable for initial testing.

import os
from energi import EnergiClient

# Replace with your actual API key or set as an environment variable
API_KEY = os.environ.get("ENERGI_API_KEY", "YOUR_ENERGI_API_KEY") 

if API_KEY == "YOUR_ENERGI_API_KEY":
    print("Warning: Please replace 'YOUR_ENERGI_API_KEY' with your actual API key or set the ENERGI_API_KEY environment variable.")
    exit()

client = EnergiClient(api_key=API_KEY)

try:
    # Get current electricity spot prices for Denmark (DK1 or DK2)
    # For a list of available areas, refer to Energi's API documentation.
    spot_prices = client.get_spot_prices(area="DK1", date="today")
    
    print("Current Electricity Spot Prices (DK1):")
    for price_entry in spot_prices:
        print(f"  Time: {price_entry['timestamp']}, Price: {price_entry['price_dkk_per_kwh']} DKK/kWh")

    # Example: Get gas prices for a specific product
    # For available products, check the Energi API documentation.
    # gas_prices = client.get_gas_prices(product="naturgas", date="today")
    # print("\nCurrent Gas Prices (Naturgas):")
    # for price_entry in gas_prices:
    #     print(f"  Time: {price_entry['timestamp']}, Price: {price_entry['price_dkk_per_m3']} DKK/m³")

except Exception as e:
    print(f"An error occurred: {e}")

This Python snippet initializes the EnergiClient with an API key and then calls the get_spot_prices method to fetch data. The response is a list of dictionaries, each containing timestamp and price information. Error handling is included to catch potential issues during the API call.

JavaScript Quickstart (Node.js)

Assuming you have installed @energi/js-sdk:

const { EnergiClient } = require('@energi/js-sdk');

const API_KEY = process.env.ENERGI_API_KEY || 'YOUR_ENERGI_API_KEY';

if (API_KEY === 'YOUR_ENERGI_API_KEY') {
  console.warn("Warning: Please replace 'YOUR_ENERGI_API_KEY' with your actual API key or set the ENERGI_API_KEY environment variable.");
  process.exit();
}

const client = new EnergiClient(API_KEY);

async function getSpotPrices() {
  try {
    const spotPrices = await client.getSpotPrices('DK1', 'today');
    console.log('Current Electricity Spot Prices (DK1):');
    spotPrices.forEach(priceEntry => {
      console.log(`  Time: ${priceEntry.timestamp}, Price: ${priceEntry.price_dkk_per_kwh} DKK/kWh`);
    });
  } catch (error) {
    console.error(`An error occurred: ${error.message}`);
  }
}

getSpotPrices();

PHP Quickstart

Assuming you have installed energi/php-sdk:

<?php
require 'vendor/autoload.php';

use Energi\EnergiClient;

$apiKey = getenv('ENERGI_API_KEY') ?: 'YOUR_ENERGI_API_KEY';

if ($apiKey === 'YOUR_ENERGI_API_KEY') {
    echo "Warning: Please replace 'YOUR_ENERGI_API_KEY' with your actual API key or set the ENERGI_API_KEY environment variable.\n";
    exit();
}

$client = new EnergiClient($apiKey);

try {
    $spotPrices = $client->getSpotPrices('DK1', 'today');
    
    echo "Current Electricity Spot Prices (DK1):\n";
    foreach ($spotPrices as $priceEntry) {
        echo "  Time: " . $priceEntry['timestamp'] . ", Price: " . $priceEntry['price_dkk_per_kwh'] . " DKK/kWh\n";
    }
} catch (Exception $e) {
    echo "An error occurred: " . $e->getMessage() . "\n";
}

?>

Ruby Quickstart

Assuming you have installed energi-ruby:

require 'energi-ruby'

api_key = ENV['ENERGI_API_KEY'] || 'YOUR_ENERGI_API_KEY'

if api_key == 'YOUR_ENERGI_API_KEY'
  warn "Warning: Please replace 'YOUR_ENERGI_API_KEY' with your actual API key or set the ENERGI_API_KEY environment variable."
  exit
end

client = EnergiRuby::Client.new(api_key: api_key)

begin
  spot_prices = client.get_spot_prices(area: 'DK1', date: 'today')
  
  puts "Current Electricity Spot Prices (DK1):"
  spot_prices.each do |price_entry|
    puts "  Time: #{price_entry['timestamp']}, Price: #{price_entry['price_dkk_per_kwh']} DKK/kWh"
  end
rescue StandardError => e
  puts "An error occurred: #{e.message}"
end

Community libraries

While Energi provides official SDKs, the open-source community may also contribute additional libraries or wrappers. These community-driven projects can offer alternative functionalities, integrate with specific frameworks, or provide utilities not covered by the official SDKs. Community libraries are not officially supported or maintained by Energi, but they can sometimes offer solutions for niche use cases or preferred development environments. Developers interested in exploring community contributions can often find them on platforms like GitHub by searching for "Energi API" or "Energi SDK" combined with their preferred programming language or framework.

When considering community libraries, it is important to evaluate their maintenance status, documentation quality, and compatibility with the latest Energi API versions. Consulting the community for reviews or checking the project's commit history can provide insights into its reliability and ongoing support. For critical applications, relying on the official SDKs is generally recommended due to their direct support from Energi.