Optimize by time duration
curl --request POST \
--url https://energy-api.flatpeak.com/devices/{device_id}/optimizations/duration \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"start_time": "2026-06-15T09:00:00Z",
"end_time": "2026-06-16T09:00:00Z",
"rank_by": "PRICE",
"import": {
"segment": "LOW",
"duration": 45
},
"export": {
"segment": "HIGH",
"duration": 45
}
}
'import requests
url = "https://energy-api.flatpeak.com/devices/{device_id}/optimizations/duration"
payload = {
"start_time": "2026-06-15T09:00:00Z",
"end_time": "2026-06-16T09:00:00Z",
"rank_by": "PRICE",
"import": {
"segment": "LOW",
"duration": 45
},
"export": {
"segment": "HIGH",
"duration": 45
}
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
start_time: '2026-06-15T09:00:00Z',
end_time: '2026-06-16T09:00:00Z',
rank_by: 'PRICE',
import: {segment: 'LOW', duration: 45},
export: {segment: 'HIGH', duration: 45}
})
};
fetch('https://energy-api.flatpeak.com/devices/{device_id}/optimizations/duration', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://energy-api.flatpeak.com/devices/{device_id}/optimizations/duration",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'start_time' => '2026-06-15T09:00:00Z',
'end_time' => '2026-06-16T09:00:00Z',
'rank_by' => 'PRICE',
'import' => [
'segment' => 'LOW',
'duration' => 45
],
'export' => [
'segment' => 'HIGH',
'duration' => 45
]
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://energy-api.flatpeak.com/devices/{device_id}/optimizations/duration"
payload := strings.NewReader("{\n \"start_time\": \"2026-06-15T09:00:00Z\",\n \"end_time\": \"2026-06-16T09:00:00Z\",\n \"rank_by\": \"PRICE\",\n \"import\": {\n \"segment\": \"LOW\",\n \"duration\": 45\n },\n \"export\": {\n \"segment\": \"HIGH\",\n \"duration\": 45\n }\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://energy-api.flatpeak.com/devices/{device_id}/optimizations/duration")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"start_time\": \"2026-06-15T09:00:00Z\",\n \"end_time\": \"2026-06-16T09:00:00Z\",\n \"rank_by\": \"PRICE\",\n \"import\": {\n \"segment\": \"LOW\",\n \"duration\": 45\n },\n \"export\": {\n \"segment\": \"HIGH\",\n \"duration\": 45\n }\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://energy-api.flatpeak.com/devices/{device_id}/optimizations/duration")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"start_time\": \"2026-06-15T09:00:00Z\",\n \"end_time\": \"2026-06-16T09:00:00Z\",\n \"rank_by\": \"PRICE\",\n \"import\": {\n \"segment\": \"LOW\",\n \"duration\": 45\n },\n \"export\": {\n \"segment\": \"HIGH\",\n \"duration\": 45\n }\n}"
response = http.request(request)
puts response.read_body{
"id": "opt_65e42cdd73e9f861edda35d7",
"object": "optimization_duration",
"live_mode": true,
"device_id": "dev_63a6087272941ef077a8fd3e",
"location_id": "loc_65e42ce4d3b813479b252160",
"timezone": "Europe/London",
"currency_code": "EUR",
"data": [
{
"start_time": "2026-05-20T00:00:00Z",
"end_time": "2026-05-20T05:30:00Z",
"price": {
"value": 0.032,
"confidence": 1
},
"solar": {
"energy_kwh": 1.41,
"power_kw": 2.82,
"confidence": 0.66
}
}
],
"account_id": "acc_661677911f2197045e6cf1b1",
"time_created": "2026-05-23T15:14:22Z"
}{
"object": "error",
"type": "invalid_request",
"code": "not_found",
"message": "The requested resource could not be found.",
"context": "api.price_get",
"live_mode": true,
"time_created": "2026-01-01T00:00:00Z"
}{
"object": "error",
"type": "invalid_request",
"code": "not_found",
"message": "The requested resource could not be found.",
"context": "api.price_get",
"live_mode": true,
"time_created": "2026-01-01T00:00:00Z"
}{
"object": "error",
"type": "invalid_request",
"code": "not_found",
"message": "The requested resource could not be found.",
"context": "api.price_get",
"live_mode": true,
"time_created": "2026-01-01T00:00:00Z"
}{
"object": "error",
"type": "invalid_request",
"code": "not_found",
"message": "The requested resource could not be found.",
"context": "api.price_get",
"live_mode": true,
"time_created": "2026-01-01T00:00:00Z"
}{
"object": "error",
"type": "invalid_request",
"code": "not_found",
"message": "The requested resource could not be found.",
"context": "api.price_get",
"live_mode": true,
"time_created": "2026-01-01T00:00:00Z"
}{
"object": "error",
"type": "invalid_request",
"code": "not_found",
"message": "The requested resource could not be found.",
"context": "api.price_get",
"live_mode": true,
"time_created": "2026-01-01T00:00:00Z"
}{
"object": "error",
"type": "invalid_request",
"code": "not_found",
"message": "The requested resource could not be found.",
"context": "api.price_get",
"live_mode": true,
"time_created": "2026-01-01T00:00:00Z"
}{
"object": "error",
"type": "invalid_request",
"code": "not_found",
"message": "The requested resource could not be found.",
"context": "api.price_get",
"live_mode": true,
"time_created": "2026-01-01T00:00:00Z"
}Optimizations
Optimize by time duration
Returns the cheapest or most valuable intervals that add up to the required duration within the window, or with rank_by SOLAR the sunniest. Intervals may be non-contiguous. Each window carries the solar forecast for its interval.
POST
/
devices
/
{device_id}
/
optimizations
/
duration
Optimize by time duration
curl --request POST \
--url https://energy-api.flatpeak.com/devices/{device_id}/optimizations/duration \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"start_time": "2026-06-15T09:00:00Z",
"end_time": "2026-06-16T09:00:00Z",
"rank_by": "PRICE",
"import": {
"segment": "LOW",
"duration": 45
},
"export": {
"segment": "HIGH",
"duration": 45
}
}
'import requests
url = "https://energy-api.flatpeak.com/devices/{device_id}/optimizations/duration"
payload = {
"start_time": "2026-06-15T09:00:00Z",
"end_time": "2026-06-16T09:00:00Z",
"rank_by": "PRICE",
"import": {
"segment": "LOW",
"duration": 45
},
"export": {
"segment": "HIGH",
"duration": 45
}
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
start_time: '2026-06-15T09:00:00Z',
end_time: '2026-06-16T09:00:00Z',
rank_by: 'PRICE',
import: {segment: 'LOW', duration: 45},
export: {segment: 'HIGH', duration: 45}
})
};
fetch('https://energy-api.flatpeak.com/devices/{device_id}/optimizations/duration', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://energy-api.flatpeak.com/devices/{device_id}/optimizations/duration",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'start_time' => '2026-06-15T09:00:00Z',
'end_time' => '2026-06-16T09:00:00Z',
'rank_by' => 'PRICE',
'import' => [
'segment' => 'LOW',
'duration' => 45
],
'export' => [
'segment' => 'HIGH',
'duration' => 45
]
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://energy-api.flatpeak.com/devices/{device_id}/optimizations/duration"
payload := strings.NewReader("{\n \"start_time\": \"2026-06-15T09:00:00Z\",\n \"end_time\": \"2026-06-16T09:00:00Z\",\n \"rank_by\": \"PRICE\",\n \"import\": {\n \"segment\": \"LOW\",\n \"duration\": 45\n },\n \"export\": {\n \"segment\": \"HIGH\",\n \"duration\": 45\n }\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://energy-api.flatpeak.com/devices/{device_id}/optimizations/duration")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"start_time\": \"2026-06-15T09:00:00Z\",\n \"end_time\": \"2026-06-16T09:00:00Z\",\n \"rank_by\": \"PRICE\",\n \"import\": {\n \"segment\": \"LOW\",\n \"duration\": 45\n },\n \"export\": {\n \"segment\": \"HIGH\",\n \"duration\": 45\n }\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://energy-api.flatpeak.com/devices/{device_id}/optimizations/duration")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"start_time\": \"2026-06-15T09:00:00Z\",\n \"end_time\": \"2026-06-16T09:00:00Z\",\n \"rank_by\": \"PRICE\",\n \"import\": {\n \"segment\": \"LOW\",\n \"duration\": 45\n },\n \"export\": {\n \"segment\": \"HIGH\",\n \"duration\": 45\n }\n}"
response = http.request(request)
puts response.read_body{
"id": "opt_65e42cdd73e9f861edda35d7",
"object": "optimization_duration",
"live_mode": true,
"device_id": "dev_63a6087272941ef077a8fd3e",
"location_id": "loc_65e42ce4d3b813479b252160",
"timezone": "Europe/London",
"currency_code": "EUR",
"data": [
{
"start_time": "2026-05-20T00:00:00Z",
"end_time": "2026-05-20T05:30:00Z",
"price": {
"value": 0.032,
"confidence": 1
},
"solar": {
"energy_kwh": 1.41,
"power_kw": 2.82,
"confidence": 0.66
}
}
],
"account_id": "acc_661677911f2197045e6cf1b1",
"time_created": "2026-05-23T15:14:22Z"
}{
"object": "error",
"type": "invalid_request",
"code": "not_found",
"message": "The requested resource could not be found.",
"context": "api.price_get",
"live_mode": true,
"time_created": "2026-01-01T00:00:00Z"
}{
"object": "error",
"type": "invalid_request",
"code": "not_found",
"message": "The requested resource could not be found.",
"context": "api.price_get",
"live_mode": true,
"time_created": "2026-01-01T00:00:00Z"
}{
"object": "error",
"type": "invalid_request",
"code": "not_found",
"message": "The requested resource could not be found.",
"context": "api.price_get",
"live_mode": true,
"time_created": "2026-01-01T00:00:00Z"
}{
"object": "error",
"type": "invalid_request",
"code": "not_found",
"message": "The requested resource could not be found.",
"context": "api.price_get",
"live_mode": true,
"time_created": "2026-01-01T00:00:00Z"
}{
"object": "error",
"type": "invalid_request",
"code": "not_found",
"message": "The requested resource could not be found.",
"context": "api.price_get",
"live_mode": true,
"time_created": "2026-01-01T00:00:00Z"
}{
"object": "error",
"type": "invalid_request",
"code": "not_found",
"message": "The requested resource could not be found.",
"context": "api.price_get",
"live_mode": true,
"time_created": "2026-01-01T00:00:00Z"
}{
"object": "error",
"type": "invalid_request",
"code": "not_found",
"message": "The requested resource could not be found.",
"context": "api.price_get",
"live_mode": true,
"time_created": "2026-01-01T00:00:00Z"
}{
"object": "error",
"type": "invalid_request",
"code": "not_found",
"message": "The requested resource could not be found.",
"context": "api.price_get",
"live_mode": true,
"time_created": "2026-01-01T00:00:00Z"
}Body
application/json
What ranks the intervals: PRICE (default) uses the tariff, SOLAR uses the generation forecast and returns the intervals with the most generation adding up to the duration. SOLAR requires forecasts on the device or its location.
Available options:
PRICE, SOLAR Example:
"PRICE"
Response
Optimization by duration response.
Was this page helpful?

