wss://api.plugchoice.com/v4/websocket sends you the live state of chargers as it changes. You subscribe once, get each charger’s state now, then its whole state again after each change. A charger’s state is the JSON that GET /v4/chargers/{charger}/live answers, so one parser reads both.
Poll or subscribe
PollGET /v4/chargers/{charger}/live, or GET /v4/chargers/live for several chargers, when you need the state now and then: a script that runs every few minutes, a page that shows the state when it opens, or code that can’t keep a connection open.
Subscribe when you act on changes as they happen, such as on a dashboard that stays open, in Home Assistant or in evcc. A change reaches you without waiting for your next poll.
The WebSocket only reads. To change something, such as an EVSE’s limit, send an HTTP request.
Connect
Open a WebSocket towss://api.plugchoice.com/v4/websocket, with nothing in the URL. Your token goes in the first message, never in the URL: proxies along the way log URLs, and browsers keep them in their history. A browser’s WebSocket can’t send an Authorization header, so a first message works the same for every client. The API doesn’t read an Authorization header on the upgrade.
The API may refuse the upgrade with a problem:
Every message, both ways, is a JSON object in a text frame:
A message with another field gets
validation-failed. A binary frame, or text that isn’t a JSON object with a type, closes the connection with 4000. Ignore fields, message types and reasons you don’t know: like the rest of v4, the protocol only adds.
Right after the upgrade, the server sends hello:
connection_id when you ask support about a connection.
Authenticate
Within 10 seconds ofhello, and before any other message, send authenticate with an access token. It’s the token you send to the HTTP API: see authentication.
expires_at is when the connection’s authentication ends: the token’s expiry, or the end of a personal API token when that comes first. scopes are the token’s scopes. The id is optional.
- A token the API doesn’t accept gets an
errorwithunauthenticated, and the connection closes with4002. So does a client’s own token, which belongs to your OAuth client and not to a person. Every HTTP endpoint exceptGET /tokenrefuses it too. - No
authenticatewithin 10 seconds, or another message first, closes the connection with4001.
Subscribe
Sendsubscribe with an id of your own. The id names the subscription: every message of it carries the id.
chargers/live follows the chargers that GET /v4/chargers/live lists, with its scope (chargers:read), access and reach. Its filter takes the list’s filters that say where:
Filters narrow each other. Without a filter, the subscription follows every charger your token reaches, when that is at most 100. The list’s other filters, such as
status, aren’t taken: they change with the state the subscription sends.
Instead of subscribed, you get an error with the subscription’s id when:
To end a subscription, send
unsubscribe with its id:
not-found. After unsubscribed, you can use the id again.
What a subscription sends
subscribed, withcount: how many chargers it follows.- A
liveper charger with its state now, in the list’s order. - A
liveafter each change, with the charger’s whole state. Replace what you had with it. - At most one
liveper charger a second. A charger that changes more often sends its newest state at the end of the second. A change that undoes itself within the second sends nothing. removedwith acharger_idwhen a charger leaves a location’s or a team’s subscription, and alivewhen a charger comes in. The API checks this every 60 seconds.unsubscribedwith areasonwhen the subscription ends. No message of the subscription comes after it.
Messages arrive in the order the server sent them. Between subscriptions there is no order beyond that: compare the times in the state, such as
measured_at and status_changed_at.
Chargers you name with charger_id stay watched while the subscription is open: while they charge, they send their meter values every 60 seconds, so their power and current change more often. A subscription of a location or a team keeps no charger watched.
There is no resume. On a new connection, subscribe again: the first messages are the state now. Changes in between aren’t sent, and a charger missing from the new first messages is no longer in the subscription. A session that started and ended while you were away is in no live message. Read those with GET /v4/transactions and updated_since.
Renew the token
60 seconds beforeexpires_at, the server sends token_expiring. Send authenticate again with a fresh access token. The connection and its subscriptions go on.
expires_at.
- The fresh token must be for the same person and the same OAuth client. Another token gets
forbidden, and the connection keeps its old one. To change users, open a new connection. - A fresh token the API doesn’t accept gets
unauthenticated. The connection goes on with its old token untilexpires_at. - Without a fresh token, the connection closes with
4100atexpires_at. A renewal can’t take a connection past the end of a personal API token. - Don’t give
authenticatethe id of an open subscription: it getsduplicate-id, and the token isn’t renewed.
removed or unsubscribed.
Heartbeats
- The server sends
heartbeatafter 25 seconds without another message (heartbeat_interval_sinhello). - Nothing from the server for 60 seconds, not even a
heartbeat, means the connection is gone, even when your socket doesn’t say so. Close it and reconnect. - The server sends a WebSocket Ping frame every 25 seconds. Browsers and WebSocket libraries answer it by themselves. A connection the server hears nothing from for 60 seconds, not even that answer, closes with
4102. - Read messages as they come. A connection that takes no message for 60 seconds closes with
4103.
ping. A pong comes back with the same data and id:
Reconnect
Before the server closes a connection, it sendsreconnect: when the server stops, such as for a release, or when the connection reaches its maximum age, after 50 to 60 minutes.
- Open a new connection, authenticate and subscribe, as at the start.
- Once the new subscriptions are made, close the old connection with code
1000. Until then both may send the same state: keep the new connection’s. - The server closes the old connection after
close_in_sseconds, with4200or4201.
Backoff means a random wait before each attempt, up to 2 seconds before the first, then up to 4, 8 and so on, at most 60. Start counting again once a connection has been authenticated for a minute.
Limits
Each personal API token counts as an OAuth client of its own. A connection that was sent
reconnect doesn’t count toward the connection limits, so the new one you open in its place fits.
Try it
websocat sends each line you give it as a message, and prints each message it gets. This sendsauthenticate with the token in $ACCESS_TOKEN, then subscribe, then each line you type:
4100 when the token runs out.
Examples
Both examples follow the chargers at one location and print each EVSE’s status and power. They renew the token ontoken_expiring, move to a new connection on reconnect, reconnect with backoff by the close code, and give up on a connection that sends nothing for 60 seconds. They get their token with a personal API token’s client ID and secret: see authentication.
The JavaScript example uses the standard WebSocket, so it runs in Node.js 22 or later and in a browser on an origin the API allows. In a browser, make accessToken() return the access token of your app’s sign-in: a client secret never goes in code that runs in a browser.
The Python example needs Python 3.11 or later and the websockets library, version 14 or later (pip install websockets).
Integrations that run all day
An integration for Home Assistant, evcc or another server keeps one connection open and does what the examples do.- Put your subscriptions on one connection, up to 20, rather than a connection per charger. Connections are limited per OAuth client and person.
- On
token_expiring, get a fresh token the way you get one for HTTP: with the refresh token of the user’s sign-in, or with your personal API token’s client ID and secret. - Name the chargers with
charger_idwhen you need their power every minute while they charge. A subscription of a location or a team keeps no charger watched. - On a close from
4000to4099, stop reconnecting and tell the user what to fix, such as signing in again. - To record sessions, read
GET /v4/transactionswithupdated_sinceafter each reconnect. A session that started and ended while you were away is in nolivemessage. - Log the
connection_idfromhello, and give it when you ask support about a connection.