Skip to content

Read the Polestar Data Portal's M2M API - #81

Merged
vcanuel merged 1 commit into
mainfrom
polestar-client
Oct 20, 2026
Merged

vcanuel merged 1 commit into
mainfrom
polestar-client

Conversation

@vcanuel

@vcanuel vcanuel commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Part of #52 (step 2: the client). It does not close the issue: the schema, the collector by provider, the API and the Connection page come next.

What

internal/polestar, the client of the Polestar Data Portal's M2M API, on the model of internal/volvo. Nothing uses it yet.

  • Client: Token (POST /token, clientId/clientSecret in JSON, without grant_type), Vehicles (VINs only), Fetch of a domain (battery, odometer, location, availability, target-soc), with Authorization: Bearer and x-client-id. One request per call, no retry, body capped, JSON checked.
  • Typed errors (APIError, Kind, RetryIn): credential refused (401 on /token, or AUTHZ_CLIENT_ID_MISMATCH), token expired (401 on /v1), VIN no longer shared, domain not granted, DATA_NOT_AVAILABLE, 429 with Retry-After (seconds or a date).
  • polestar.Normalizer (satisfies derive.Normalizer, source polestar): values dated by the car's timestamp, odometer in metres → km, usageMode → engine, chargingStatusV2 → charging status, protobuf's omitted zeros (a missing power is 0 W only outside an active charge), model year from the VIN's 10th character, battery electric.
  • Fingerprint without timestamp, metaReceivedAt and metaEventId.
  • archtest allowlist and a depguard rule polestar-is-a-client; core and api may not import it.

Assumptions and what is left open

  • The specification describes no 401 on /v1: any 401 there is taken for an expired token.
  • A 403 is "domain not granted" only on a domain fetch with the API's error body (the code of a missing scope is not published).
  • SMART_CHARGING_WILL_NOT_FINISH is counted as charging; CHARGING_TYPE_WIRELESS is left absent.
  • The family is left unknown: no real VIN of a Polestar 3, 4 or 5 tells them apart. Choosing a variant then needs the API to offer the brand's variants when the family is unknown: a later step, noted on the issue.

Tests

  • Real responses of testdata/polestar-data-portal/ (exact snapshots, the real 404 body).
  • The simulator (polestarapi, scenario polestar2): token, vehicles, every domain; parked, driving, on a DC charge; each error the simulator gives; a parked car's identical re-upload keeps its fingerprint.
  • task unit-go passes. Mutation check: an import of volvo in polestar fails both depguard and TestDependencies.

🤖 Generated with Claude Code

A Polestar is read through the Polestar Data Portal (EU Data Act), with
a credential its owner creates there. internal/polestar is its client,
on the model of internal/volvo, used by nothing yet.

The client asks a token with the credential's client ID and secret (a
JSON body, without grant_type), lists the VINs, and fetches the five
domains a logger needs (battery, odometer, location, availability,
target-soc), with the account ID in x-client-id. It returns the raw
bodies: one request per call, no retry, the body capped and checked as
JSON. Its errors are kinds the collector will act on: a refused
credential (a 401 on /token, or an account ID that is not the
credential's), an expired token (any 401 on /v1: the specification
describes none), a VIN no longer shared, a domain not granted (a 403 of
the API's own body on a domain), a domain the car does not upload
(DATA_NOT_AVAILABLE), and a 429 with Retry-After when it has one.

Normalizer reads the domains into snapshots for the source polestar,
each value dated by the car's timestamp. The API writes protobuf's JSON,
which leaves zeros out: a missing field stays absent, but the charging
power out of an active charge, which is 0 W. The enums lose their
prefix; usageMode stands for the engine, chargingStatusV2 for the
charging status. The model comes from the VIN alone: its year at the
tenth position, battery electric for every model the portal serves; the
family is left unknown, no real VIN telling the Polestar 2 from the 3,
4 or 5. The fingerprint leaves out timestamp, metaReceivedAt and
metaEventId, so that a parked car uploading the same values again makes
no new snapshot.

Tested against the real responses of testdata/polestar-data-portal and
against the simulator's Polestar of demonstration. The package joins
archtest's allowlist, with its depguard rule.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vcanuel
vcanuel merged commit 0dbb0bb into main Oct 20, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant