npm package: @launchbasex/sdk
Description: LaunchBase SDK for connecting generated backends to LaunchBase platform services
Author: LaunchBase
Latest version: 1.0.0

# @launchbasex/sdk

Official SDK for connecting generated backends to LaunchBase platform services.

## Installation

```bash
npm install @launchbasex/sdk
```

## Quick Start

```typescript
import LaunchBase from '@launchbasex/sdk'

const client = new LaunchBase({
  apiKey: process.env.LAUNCHBASE_API_KEY,
  projectId: process.env.LAUNCHBASE_PROJECT_ID,
})

// Health check
await client.healthCheck()

// Get project info
const project = await client.getProject()
```

## Authentication

```typescript
// Sign up
const { user, token } = await client.auth.signUp('user@example.com', 'password', {
  name: 'John Doe',
})

// Sign in
const auth = await client.auth.signIn('user@example.com', 'password')

// OAuth
const { url } = await client.auth.signInWithProvider('google')

// Verify token
const payload = await client.auth.verifyToken(token)

// Refresh token
const newAuth = await client.auth.refreshToken(auth.refreshToken)
```

## Database

```typescript
// Query
const users = await client.database.query('SELECT * FROM users WHERE active = $1', [true])

// CRUD
const user = await client.database.create('users', { email: 'test@example.com', name: 'Test' })
const found = await client.database.findUnique('users', user.id)
const updated = await client.database.update('users', user.id, { name: 'Updated' })
await client.database.delete('users', user.id)

// Query with options
const activeUsers = await client.database.findMany('users', {
  where: { active: true },
  orderBy: { createdAt: 'desc' },
  take: 10,
})

// Transaction
await client.database.transaction(async (tx) => {
  const user = await tx.create('users', { email: 'test@example.com' })
  await tx.create('profiles', { userId: user.id, bio: 'Hello' })
})
```

## Storage

```typescript
// Upload file
const file = await client.storage.upload('avatars/user.jpg', buffer, {
  contentType: 'image/jpeg',
  public: true,
})

// Download
const data = await client.storage.download('avatars/user.jpg')

// Get signed URL
const url = await client.storage.getSignedUrl('private/doc.pdf', 3600)

// List files
const files = await client.storage.list('avatars/')

// Delete
await client.storage.delete('avatars/user.jpg')
```

## Realtime

```typescript
// Subscribe to channel
const unsubscribe = client.realtime.subscribe('chat', (event) => {
  console.log('New message:', event.data)
})

// Publish event
await client.realtime.publish('chat', {
  type: 'message',
  data: { text: 'Hello world!' },
})

// Unsubscribe
unsubscribe()
```

## Webhooks

```typescript
// Create webhook
const webhook = await client.webhooks.create('https://example.com/webhook', [
  'user.created',
  'user.updated',
])

// List webhooks
const webhooks = await client.webhooks.list()

// Verify incoming webhook signature
import { WebhookClient } from '@launchbase/sdk/webhooks'

const { valid, event } = WebhookClient.constructEvent(
  payload,
  signature,
  webhook.secret,
  timestamp
)

if (valid) {
  console.log('Event:', event)
}
```

## Configuration

| Option | Required | Default | Description |
|--------|----------|---------|-------------|
| `apiKey` | Yes | - | Your project API key |
| `projectId` | Yes | - | Your project ID |
| `apiUrl` | No | `https://api.launchbase.dev` | API base URL |
| `timeout` | No | `30000` | Request timeout in ms |
| `retries` | No | `3` | Number of retries on failure |

## Environment Variables

```bash
LAUNCHBASE_API_KEY=your_api_key
LAUNCHBASE_PROJECT_ID=your_project_id
LAUNCHBASE_API_URL=https://api.launchbase.dev  # optional
```

## License

MIT