npm package: @letsparky/api-client
Description: TypeScript SDK for Letsparky API
Latest version: 1.0.2

# Letsparky TypeScript API Client

A TypeScript client for interacting with the Letsparky User API.

## Installation

```bash
npm install @letsparky/api-client
```

## Usage

```typescript
import { LetsparkyClient } from 'letsparky-api-client';

// Initialize the client
const client = new LetsparkyClient({
  baseUrl: 'https://api.letsparky.com',
  // Either use an API key
  apiKey: 'your-api-key',
  // Or use access token (typically after login)
  // accessToken: 'your-access-token',
  // refreshToken: 'your-refresh-token'
});
```

## Authentication

### Login with Email and Password

```typescript
const loginResponse = await client.auth.login({
  email: 'user@example.com',
  password: 'password123'
});

// The client automatically stores the tokens
console.log(loginResponse.userId);
console.log(loginResponse.accessToken);
```

### Refresh Token

```typescript
const refreshResponse = await client.auth.refreshToken();
console.log(refreshResponse.accessToken);
console.log(refreshResponse.refreshToken);
```

### Set Authentication Manually

```typescript
// Set access token
client.setAccessToken('your-access-token', 'your-refresh-token');

// Or set API key
client.setApiKey('your-api-key');
```

## Device Management

### Create a Device

```typescript
const createDeviceResponse = await client.device.createDevice({
  serialNo: 'DEVICE123',
  type: 'PARKY_TCP',
  terminalArmonId: 'ARMON123',
  firmwareVersion: '1.0.0',
  macAddress: '00:11:22:33:44:55',
  imeiNumber: '123456789012345',
  blePassword: 'password123'
});

console.log(createDeviceResponse.deviceId);
```

### Get Device by ID

```typescript
const device = await client.device.getDeviceById('device-id');
```

### Get Device by QR Data

```typescript
const device = await client.device.getDeviceByQRData('qr-data');
```

## User Device Management

### Get User Devices

```typescript
const userDevices = await client.userDevice.getUserDevices();
```

### Add Owner to Device

```typescript
const addDeviceResponse = await client.userDevice.addOwnerToDevice({
  serialNo: 'DEVICE123',
  nickName: 'My Device',
  qrData: 'qr-data',
  terminalType: 'type',
  photo: 'base64-encoded-photo',
  geoLat: 40.7128,
  geoLng: -74.0060
});

console.log(addDeviceResponse.id);
console.log(addDeviceResponse.deviceId);
```

### Factory Reset a Device

```typescript
const resetResponse = await client.userDevice.factoryResetDevice('device-id');
console.log(resetResponse.success);
```

### Get User Device by Device ID

```typescript
const userDevice = await client.userDevice.getUserDeviceByDeviceId('device-id');
```

### Get User Device by QR Data

```typescript
const userDevice = await client.userDevice.getUserDeviceByQrData('qr-data');
```

## TCP Device Operations

### Get Operational Status

```typescript
const status = await client.tcpDevice.getOperationalStatus('device-id');
console.log(status.operationalStatus);
console.log(status.batteryVoltage);
```

### Block Device

```typescript
const blockResponse = await client.tcpDevice.blockDevice('device-id');
console.log(blockResponse.command);
```

### Unblock Device

```typescript
const unblockResponse = await client.tcpDevice.unblockDevice('device-id');
console.log(unblockResponse.command);
```

## Webhook Configuration

### Create Webhook Configuration

```typescript
const webhookConfig = await client.webhookConfiguration.createWebhookConfiguration({
  callbackUrl: 'https://your-callback-url.com/webhook'
});
```

### Update Webhook Configuration

```typescript
const updatedConfig = await client.webhookConfiguration.updateWebhookConfiguration({
  id: 'config-id',
  callbackUrl: 'https://your-new-callback-url.com/webhook'
});
```

### Get Webhook Configuration

```typescript
const config = await client.webhookConfiguration.getWebhookConfiguration();
console.log(config.callbackUrl);
```

## Error Handling

```typescript
try {
  const response = await client.device.getDeviceById('invalid-id');
} catch (error) {
  console.error(error.message);
}
```

## Models

The client includes TypeScript interfaces for all request and response objects, ensuring type safety throughout your application.

## License

MIT