Skip to content

Commit 79f7b9a

Browse files
committed
Update API docs
1 parent dacf165 commit 79f7b9a

File tree

1 file changed

+96
-52
lines changed

1 file changed

+96
-52
lines changed

packages/api/README.md

Lines changed: 96 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
# Teslemetry TypeScript API
1+
# Teslemetry TypeScript SDK
22

33
The official TypeScript/JavaScript client for the [Teslemetry](https://teslemetry.com) API.
44

5-
This library provides a convenient wrapper for interacting with Tesla vehicles and energy sites via the Teslemetry service, including support for standard API commands and real-time streaming data (Server-Sent Events).
5+
This library provides a strictly typed, easy-to-use wrapper for interacting with Tesla vehicles and energy sites. It supports standard API commands, state retrieval, and real-time streaming data via Server-Sent Events (SSE).
66

7-
Further information about each API method can be found in the [Teslemetry API Documentation](https://teslemetry.com/docs/api), and [Tesla Fleet API Documentation](https://developer.tesla.com/docs/fleet-api/endpoints/vehicle-commands).
7+
## 📚 Documentation
88

9-
## Features
10-
11-
- 🚗 **Vehicle API**: Full control and state retrieval (lock/unlock, climate, charging, etc.).
12-
-**Energy API**: Monitor and control Tesla Energy sites (Solar, Powerwall).
13-
- 📡 **Streaming (SSE)**: Real-time vehicle data streaming with `TeslemetryStream`.
14-
- 🌍 **Region Aware**: Automatic region detection and handling (NA/EU).
15-
- 🔒 **Type-Safe**: Built with TypeScript for full type inference and safety.
9+
- **[Teslemetry API Reference](https://teslemetry.com/docs/api)**: Detailed documentation for all API endpoints, parameters, and response values.
10+
- **[Tesla Fleet API](https://developer.tesla.com/docs/fleet-api)**: Official Tesla documentation for underlying vehicle commands and data.
1611

1712
## Installation
1813

@@ -29,100 +24,149 @@ yarn add @teslemetry/api
2924
```typescript
3025
import { Teslemetry } from "@teslemetry/api";
3126

32-
const token = process.env.TESLEMETRY_ACCESS_TOKEN;
33-
const vin = process.env.TESLEMETRY_VIN;
34-
35-
const teslemetry = new Teslemetry(token);
27+
// Initialize with your access token
28+
const teslemetry = new Teslemetry(process.env.TESLEMETRY_ACCESS_TOKEN);
3629

37-
// Get a vehicle instance
30+
// Get a specific vehicle
31+
const vin = "5YJ...";
3832
const vehicle = teslemetry.getVehicle(vin);
3933

40-
// API: Get vehicle state
41-
const {response} = await vehicle.api.state();
42-
console.log("State:", response.state);
34+
// 1. Get Vehicle State
35+
const state = await vehicle.api.state();
36+
console.log("Vehicle State:", state);
4337

44-
// API: Send a command
38+
// 2. Send a Command (e.g., Flash Lights)
4539
await vehicle.api.flashLights();
4640

47-
// Stream: Listen for real-time data
41+
// 3. Stream Real-time Data
4842
vehicle.sse.onSignal("Speed", (speed) => {
49-
console.log(`Current Speed: ${speed}`);
43+
console.log(`Current Speed: ${speed} mph`);
5044
});
5145

46+
// Connect to the stream
5247
await teslemetry.sse.connect();
5348
```
5449

5550
## Usage
5651

5752
### Initialization
5853

59-
Initialize the `Teslemetry` client with your access token. You can optionally specify a region ("na" or "eu"), otherwise it will be automatically detected.
54+
The `Teslemetry` class is the main entry point. It automatically handles region detection (NA/EU) upon the first request, or you can specify it manually.
6055

6156
```typescript
6257
import { Teslemetry } from "@teslemetry/api";
6358

59+
// Automatic region detection (recommended)
6460
const teslemetry = new Teslemetry("YOUR_ACCESS_TOKEN");
65-
// or with specific region
66-
const teslemetryEu = new Teslemetry("YOUR_ACCESS_TOKEN", "eu");
61+
62+
// Manual region specification
63+
const teslemetryEu = new Teslemetry("YOUR_ACCESS_TOKEN", { region: "eu" });
6764
```
6865

69-
### Vehicle Control
66+
### Vehicle API
7067

71-
The `getVehicle(vin)` method returns an object containing both `api` and `sse` handlers for a specific vehicle.
68+
Use `getVehicle(vin)` to interact with a vehicle. This returns an object containing two specialized handlers:
69+
- `api`: for standard REST API calls (commands, state).
70+
- `sse`: for real-time streaming.
71+
72+
#### Commands & State
73+
The `.api` property contains methods for all supported Tesla commands.
7274

7375
```typescript
74-
const myCar = teslemetry.getVehicle("VIN123456789");
76+
const vehicle = teslemetry.getVehicle("VIN...");
77+
78+
// Get full vehicle data
79+
const data = await vehicle.api.vehicleData();
7580

76-
// Get vehicle state
77-
const state = await myCar.api.state();
81+
// Climate Control
82+
await vehicle.api.autoConditioningStart();
83+
await vehicle.api.setTemps(20, 20); // Driver, Passenger (Celsius)
7884

79-
// Commands
80-
await myCar.api.doorLock();
81-
await myCar.api.autoConditioningStart();
82-
await myCar.api.chargeStart();
85+
// Charging
86+
await vehicle.api.chargeStart();
87+
await vehicle.api.setChargeLimit(80);
88+
89+
// Locking
90+
await vehicle.api.lockDoors();
8391
```
8492

85-
### Real-time Streaming (SSE)
93+
> **Note:** For a comprehensive list of all available methods and their parameters, please refer to the [Teslemetry API Docs](https://teslemetry.com/docs/api). The SDK methods map 1:1 with these endpoints.
8694
87-
Teslemetry supports streaming vehicle data updates via Server-Sent Events.
95+
#### Real-time Streaming (SSE)
96+
The `.sse` property allows you to subscribe to specific vehicle signals.
8897

8998
```typescript
90-
const myCar = teslemetry.getVehicle("VIN123456789");
91-
92-
// Subscribe to specific signals
93-
myCar.sse.onSignal("PackCurrent", (val) => console.log("Current:", val));
94-
myCar.sse.onSignal("ChargerVoltage", (val) => console.log("Voltage:", val));
99+
// Subscribe to signals
100+
vehicle.sse.onSignal("PackCurrent", (val) => console.log("Current:", val));
101+
vehicle.sse.onSignal("ChargerVoltage", (val) => console.log("Voltage:", val));
95102

96-
// Handle connection status
103+
// Monitor connection status
97104
teslemetry.sse.onConnection((isConnected) => {
98-
console.log(isConnected ? "Connected!" : "Disconnected");
105+
console.log(isConnected ? "Stream Connected" : "Stream Disconnected");
99106
});
100107

101-
// Start the stream
108+
// Start streaming (connects to the shared Teslemetry stream)
102109
await teslemetry.sse.connect();
103110

104111
// Stop streaming
105112
teslemetry.sse.disconnect();
106113
```
107114

108-
### Energy Sites
115+
### Energy API
109116

110-
Interact with Tesla Energy products.
117+
Interact with Tesla Energy sites (Solar, Powerwall, Wall Connector).
111118

112119
```typescript
120+
// Get an energy site instance by Site ID
113121
const site = teslemetry.energySite(12345);
114-
const data = await site.getSiteInfo();
122+
123+
// Get site status and info
124+
const status = await site.getLiveStatus();
125+
const info = await site.getSiteInfo();
126+
127+
// Control operations
128+
await site.setBackupReserve(20); // Set backup reserve to 20%
129+
await site.setOperationMode("autonomous");
115130
```
116131

117-
## Development
132+
### Account & Discovery
118133

119-
To build the package locally:
134+
If you don't know your VINs or Site IDs, you can discover all products on your account.
120135

121-
```bash
122-
pnpm install
123-
pnpm build
136+
```typescript
137+
// Fetch all vehicles and energy sites
138+
const products = await teslemetry.createProducts();
139+
140+
// Access discovered vehicles
141+
for (const vin in products.vehicles) {
142+
const vehicle = products.vehicles[vin];
143+
console.log(`Found ${vehicle.name} (${vehicle.vin})`);
144+
145+
// Use the API immediately
146+
await vehicle.api.honkHorn();
147+
}
148+
149+
// Access discovered energy sites
150+
for (const siteId in products.energySites) {
151+
const site = products.energySites[siteId];
152+
console.log(`Found Site: ${site.name} (${site.site})`);
153+
}
154+
```
155+
156+
### Error Handling
157+
158+
The SDK throws standard Javascript `Error` objects for configuration issues and specific errors for API failures. Streaming errors (like connection drops) are emitted via the stream error handler or specific exception classes.
159+
160+
```typescript
161+
import { TeslemetryStreamConnectionError } from "@teslemetry/api";
162+
163+
try {
164+
await vehicle.api.wakeUp();
165+
} catch (error) {
166+
console.error("Failed to wake up vehicle:", error);
167+
}
124168
```
125169

126170
## License
127171

128-
Apache-2.0
172+
Apache-2.0

0 commit comments

Comments
 (0)