Retrieve latest data point
Required capabilities:
timeSeriesAcl:READ
Retrieves the latest data point in one or more time series. Note that the latest data point in a time series is the one with the highest timestamp, which is not necessarily the one that was ingested most recently.
POST
/
timeseries
/
data
/
latest
JavaScript SDK
const datapoints = await client.datapoints.retrieveLatest([
{
before: 'now',
id: 123
},
{
externalId: 'abc',
before: new Date('21 jan 2018'),
}
]);res = client.time_series.data.retrieve_latest(id=1)[0]
res = client.time_series.data.retrieve_latest(id=1, before="2d-ago")[0]
res = client.time_series.data.retrieve_latest(id=1, target_unit="temperature:deg_f")[0]
res = client.time_series.data.retrieve_latest(id=1, target_unit_system="Imperial")[0]
from cognite.client.data_classes import LatestDatapointQuery
res = client.time_series.data.retrieve_latest(id=LatestDatapointQuery(id=1, before=60_000))[0]
res = client.time_series.data.retrieve_latest(external_id=["abc", "def"])
latest_abc = res[0][0]
latest_def = res[1][0]
from datetime import datetime, timezone
id_queries = [
123,
LatestDatapointQuery(id=456, before="1w-ago"),
LatestDatapointQuery(id=789, before=datetime(2018,1,1, tzinfo=timezone.utc)),
LatestDatapointQuery(id=987, target_unit="temperature:deg_f")]
ext_id_queries = [
"foo",
LatestDatapointQuery(external_id="abc", before="3h-ago", target_unit_system="Imperial"),
LatestDatapointQuery(external_id="def", include_status=True),
LatestDatapointQuery(external_id="ghi", treat_uncertain_as_bad=False),
LatestDatapointQuery(external_id="jkl", include_status=True, ignore_bad_datapoints=False)]
res = client.time_series.data.retrieve_latest(
id=id_queries, external_id=ext_id_queries)
List<Item> byExternalIds = List.of(Item.newBuilder()
.setExternalId("10").build());
List<TimeseriesPoint> result =
client.timeseries().dataPoints()
.retrieveLatest(byExternalIds);//by list of items
List<TimeseriesPoint> result =
client.timeseries().dataPoints()
.retrieveLatest("10", "20");//by varargs of String
List<Item> byInternalIds = List.of(Item.newBuilder()
.setId(10).build());
List<TimeseriesPoint> result =
client.timeseries().dataPoints()
.retrieveLatest(byInternalIds);//by list of items
List<TimeseriesPoint> result =
client.timeseries().dataPoints()
.retrieveLatest(10, 20);//by varargs of Long
curl --request POST \
--url https://{cluster}.cognitedata.com/api/v1/projects/{project}/timeseries/data/latest \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"items": [
{
"id": 4503599627370496,
"before": "now",
"targetUnit": "temperature:deg_f",
"targetUnitSystem": "imperial",
"includeStatus": false,
"ignoreBadDataPoints": true,
"treatUncertainAsBad": true
}
],
"ignoreUnknownIds": false
}
'<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://{cluster}.cognitedata.com/api/v1/projects/{project}/timeseries/data/latest",
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([
'items' => [
[
'id' => 4503599627370496,
'before' => 'now',
'targetUnit' => 'temperature:deg_f',
'targetUnitSystem' => 'imperial',
'includeStatus' => false,
'ignoreBadDataPoints' => true,
'treatUncertainAsBad' => true
]
],
'ignoreUnknownIds' => false
]),
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://{cluster}.cognitedata.com/api/v1/projects/{project}/timeseries/data/latest"
payload := strings.NewReader("{\n \"items\": [\n {\n \"id\": 4503599627370496,\n \"before\": \"now\",\n \"targetUnit\": \"temperature:deg_f\",\n \"targetUnitSystem\": \"imperial\",\n \"includeStatus\": false,\n \"ignoreBadDataPoints\": true,\n \"treatUncertainAsBad\": true\n }\n ],\n \"ignoreUnknownIds\": false\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))
}require 'uri'
require 'net/http'
url = URI("https://{cluster}.cognitedata.com/api/v1/projects/{project}/timeseries/data/latest")
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 \"items\": [\n {\n \"id\": 4503599627370496,\n \"before\": \"now\",\n \"targetUnit\": \"temperature:deg_f\",\n \"targetUnitSystem\": \"imperial\",\n \"includeStatus\": false,\n \"ignoreBadDataPoints\": true,\n \"treatUncertainAsBad\": true\n }\n ],\n \"ignoreUnknownIds\": false\n}"
response = http.request(request)
puts response.read_body{
"items": [
{
"id": 4503599627370496,
"isString": true,
"type": "<string>",
"datapoints": [
{
"timestamp": 1638795554528,
"value": 123,
"status": {
"code": 123,
"symbol": "<string>"
}
}
],
"externalId": "<string>",
"instanceId": {
"space": "<string>",
"externalId": "<string>"
},
"isStep": true,
"unit": "<string>",
"unitExternalId": "<string>",
"nextCursor": "<string>"
}
]
}{
"error": {
"code": 400,
"message": "<string>",
"missing": [
{
"id": 4503599627370496
}
]
}
}Authorizations
oidc-tokenoauth2-client-credentialsoauth2-open-industrial-dataoauth2-auth-code
Access token issued by the CDF project's configured identity provider. Access token must be an OpenID Connect token, and the project must be configured to accept OpenID Connect tokens. Use a header key of 'Authorization' with a value of 'Bearer $accesstoken'. The token can be obtained through any flow supported by the identity provider.
Scopes
Body
application/json
The list of the queries to perform.
⌘I
JavaScript SDK
const datapoints = await client.datapoints.retrieveLatest([
{
before: 'now',
id: 123
},
{
externalId: 'abc',
before: new Date('21 jan 2018'),
}
]);res = client.time_series.data.retrieve_latest(id=1)[0]
res = client.time_series.data.retrieve_latest(id=1, before="2d-ago")[0]
res = client.time_series.data.retrieve_latest(id=1, target_unit="temperature:deg_f")[0]
res = client.time_series.data.retrieve_latest(id=1, target_unit_system="Imperial")[0]
from cognite.client.data_classes import LatestDatapointQuery
res = client.time_series.data.retrieve_latest(id=LatestDatapointQuery(id=1, before=60_000))[0]
res = client.time_series.data.retrieve_latest(external_id=["abc", "def"])
latest_abc = res[0][0]
latest_def = res[1][0]
from datetime import datetime, timezone
id_queries = [
123,
LatestDatapointQuery(id=456, before="1w-ago"),
LatestDatapointQuery(id=789, before=datetime(2018,1,1, tzinfo=timezone.utc)),
LatestDatapointQuery(id=987, target_unit="temperature:deg_f")]
ext_id_queries = [
"foo",
LatestDatapointQuery(external_id="abc", before="3h-ago", target_unit_system="Imperial"),
LatestDatapointQuery(external_id="def", include_status=True),
LatestDatapointQuery(external_id="ghi", treat_uncertain_as_bad=False),
LatestDatapointQuery(external_id="jkl", include_status=True, ignore_bad_datapoints=False)]
res = client.time_series.data.retrieve_latest(
id=id_queries, external_id=ext_id_queries)
List<Item> byExternalIds = List.of(Item.newBuilder()
.setExternalId("10").build());
List<TimeseriesPoint> result =
client.timeseries().dataPoints()
.retrieveLatest(byExternalIds);//by list of items
List<TimeseriesPoint> result =
client.timeseries().dataPoints()
.retrieveLatest("10", "20");//by varargs of String
List<Item> byInternalIds = List.of(Item.newBuilder()
.setId(10).build());
List<TimeseriesPoint> result =
client.timeseries().dataPoints()
.retrieveLatest(byInternalIds);//by list of items
List<TimeseriesPoint> result =
client.timeseries().dataPoints()
.retrieveLatest(10, 20);//by varargs of Long
curl --request POST \
--url https://{cluster}.cognitedata.com/api/v1/projects/{project}/timeseries/data/latest \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"items": [
{
"id": 4503599627370496,
"before": "now",
"targetUnit": "temperature:deg_f",
"targetUnitSystem": "imperial",
"includeStatus": false,
"ignoreBadDataPoints": true,
"treatUncertainAsBad": true
}
],
"ignoreUnknownIds": false
}
'<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://{cluster}.cognitedata.com/api/v1/projects/{project}/timeseries/data/latest",
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([
'items' => [
[
'id' => 4503599627370496,
'before' => 'now',
'targetUnit' => 'temperature:deg_f',
'targetUnitSystem' => 'imperial',
'includeStatus' => false,
'ignoreBadDataPoints' => true,
'treatUncertainAsBad' => true
]
],
'ignoreUnknownIds' => false
]),
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://{cluster}.cognitedata.com/api/v1/projects/{project}/timeseries/data/latest"
payload := strings.NewReader("{\n \"items\": [\n {\n \"id\": 4503599627370496,\n \"before\": \"now\",\n \"targetUnit\": \"temperature:deg_f\",\n \"targetUnitSystem\": \"imperial\",\n \"includeStatus\": false,\n \"ignoreBadDataPoints\": true,\n \"treatUncertainAsBad\": true\n }\n ],\n \"ignoreUnknownIds\": false\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))
}require 'uri'
require 'net/http'
url = URI("https://{cluster}.cognitedata.com/api/v1/projects/{project}/timeseries/data/latest")
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 \"items\": [\n {\n \"id\": 4503599627370496,\n \"before\": \"now\",\n \"targetUnit\": \"temperature:deg_f\",\n \"targetUnitSystem\": \"imperial\",\n \"includeStatus\": false,\n \"ignoreBadDataPoints\": true,\n \"treatUncertainAsBad\": true\n }\n ],\n \"ignoreUnknownIds\": false\n}"
response = http.request(request)
puts response.read_body{
"items": [
{
"id": 4503599627370496,
"isString": true,
"type": "<string>",
"datapoints": [
{
"timestamp": 1638795554528,
"value": 123,
"status": {
"code": 123,
"symbol": "<string>"
}
}
],
"externalId": "<string>",
"instanceId": {
"space": "<string>",
"externalId": "<string>"
},
"isStep": true,
"unit": "<string>",
"unitExternalId": "<string>",
"nextCursor": "<string>"
}
]
}{
"error": {
"code": 400,
"message": "<string>",
"missing": [
{
"id": 4503599627370496
}
]
}
}