Skip to main content
The WebSocket at 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

Poll GET /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 to wss://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:
Give the connection_id when you ask support about a connection.

Authenticate

Within 10 seconds of hello, 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 error with unauthenticated, and the connection closes with 4002. So does a client’s own token, which belongs to your OAuth client and not to a person. Every HTTP endpoint except GET /token refuses it too.
  • No authenticate within 10 seconds, or another message first, closes the connection with 4001.

Subscribe

Send subscribe with an id of your own. The id names the subscription: every message of it carries the id.
The topic 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:
An id that names no open subscription gets not-found. After unsubscribed, you can use the id again.

What a subscription sends

  • subscribed, with count: how many chargers it follows.
  • A live per charger with its state now, in the list’s order.
  • A live after each change, with the charger’s whole state. Replace what you had with it.
  • At most one live per 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.
  • removed with a charger_id when a charger leaves a location’s or a team’s subscription, and a live when a charger comes in. The API checks this every 60 seconds.
  • unsubscribed with a reason when 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 before expires_at, the server sends token_expiring. Send authenticate again with a fresh access token. The connection and its subscriptions go on.
You may renew at any time before 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 until expires_at.
  • Without a fresh token, the connection closes with 4100 at expires_at. A renewal can’t take a connection past the end of a personal API token.
  • Don’t give authenticate the id of an open subscription: it gets duplicate-id, and the token isn’t renewed.
The API checks your access again every 60 seconds, and at once when a fresh token has other scopes or another reach. What you can no longer reach ends with removed or unsubscribed.

Heartbeats

  • The server sends heartbeat after 25 seconds without another message (heartbeat_interval_s in hello).
  • 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.
To check a connection yourself, such as when your app comes back from the background, send ping. A pong comes back with the same data and id:

Reconnect

Before the server closes a connection, it sends reconnect: when the server stops, such as for a release, or when the connection reaches its maximum age, after 50 to 60 minutes.
  1. Open a new connection, authenticate and subscribe, as at the start.
  2. 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.
  3. The server closes the old connection after close_in_s seconds, with 4200 or 4201.
Other closes tell you what to do by their code: 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 sends authenticate with the token in $ACCESS_TOKEN, then subscribe, then each line you type:
The connection closes with 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 on token_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_id when 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 4000 to 4099, stop reconnecting and tell the user what to fix, such as signing in again.
  • To record sessions, read GET /v4/transactions with updated_since after each reconnect. A session that started and ended while you were away is in no live message.
  • Log the connection_id from hello, and give it when you ask support about a connection.
The WebSocket reference lists every message and its fields.