> ## Documentation Index
> Fetch the complete documentation index at: https://docs.g-tateth.com/llms.txt
> Use this file to discover all available pages before exploring further.

# JavaScript SDK

The official g-tateth SDK for JavaScript and TypeScript.

## Installation

```bash theme={null}
npm install @g-tateth/sdk
```

Or with yarn:

```bash theme={null}
yarn add @g-tateth/sdk
```

## Quick Start

```typescript theme={null}
import { createClient } from '@g-tateth/sdk';

const client = createClient({
  apiKey: 'sk_live_your_api_key_here',
  baseURL: 'https://api.g-tateth.com' // Optional, defaults to production
});

// List conversations
const conversations = await client.conversations.list();

// Get a conversation
const conversation = await client.conversations.get('conversation_id');

// List customers
const customers = await client.customers.list();

// Create a customer
const newCustomer = await client.customers.create({
  profile: {
    firstName: 'John',
    lastName: 'Doe',
    email: 'john@example.com'
  }
});
```

## Configuration

### Options

```typescript theme={null}
interface ClientOptions {
  apiKey: string;
  baseURL?: string; // Default: 'https://api.g-tateth.com'
  timeout?: number; // Default: 30000ms
  retries?: number; // Default: 3
}
```

### Environment Variables

You can also configure the client using environment variables:

```bash theme={null}
G_TATETH_API_KEY=sk_live_your_api_key
G_TATETH_BASE_URL=https://api.g-tateth.com
```

```typescript theme={null}
const client = createClient(); // Uses environment variables
```

## API Methods

### Conversations

```typescript theme={null}
// List conversations
const conversations = await client.conversations.list({
  status: 'open',
  page: 1,
  limit: 50
});

// Get conversation
const conversation = await client.conversations.get('conversation_id');

// Create conversation
const newConversation = await client.conversations.create({
  subject: 'Customer inquiry',
  customerId: 'customer_id',
  channel: 'email'
});

// Update conversation
await client.conversations.update('conversation_id', {
  status: 'closed'
});

// Delete conversation
await client.conversations.delete('conversation_id');
```

### Customers

```typescript theme={null}
// List customers
const customers = await client.customers.list({
  search: 'john',
  page: 1,
  limit: 50
});

// Get customer
const customer = await client.customers.get('customer_id');

// Create customer
const newCustomer = await client.customers.create({
  profile: {
    firstName: 'John',
    lastName: 'Doe',
    email: 'john@example.com'
  }
});

// Update customer
await client.customers.update('customer_id', {
  profile: {
    company: 'Acme Corp'
  }
});

// Delete customer
await client.customers.delete('customer_id');
```

### Webhooks

```typescript theme={null}
// List webhooks
const webhooks = await client.webhooks.list();

// Create webhook
const webhook = await client.webhooks.create({
  name: 'My Webhook',
  url: 'https://myapp.com/webhooks',
  events: ['conversation.created', 'customer.updated']
});

// Get webhook
const webhook = await client.webhooks.get('webhook_id');

// Update webhook
await client.webhooks.update('webhook_id', {
  isActive: false
});

// Delete webhook
await client.webhooks.delete('webhook_id');
```

### Analytics

```typescript theme={null}
// Get usage statistics
const usage = await client.analytics.getUsage({
  startDate: '2024-01-01',
  endDate: '2024-01-31'
});

// Get performance metrics
const metrics = await client.analytics.getMetrics({
  days: 7
});
```

## Error Handling

The SDK throws custom error types:

```typescript theme={null}
import { GtatethError, RateLimitError, AuthenticationError } from '@g-tateth/sdk';

try {
  await client.conversations.list();
} catch (error) {
  if (error instanceof RateLimitError) {
    console.log('Rate limit exceeded:', error.retryAfter);
  } else if (error instanceof AuthenticationError) {
    console.log('Authentication failed:', error.message);
  } else if (error instanceof GtatethError) {
    console.log('API error:', error.code, error.message);
  }
}
```

## Retry Logic

The SDK automatically retries failed requests with exponential backoff:

```typescript theme={null}
const client = createClient({
  apiKey: 'sk_live_your_api_key',
  retries: 3 // Number of retry attempts
});
```

## TypeScript Support

The SDK is written in TypeScript and includes full type definitions:

```typescript theme={null}
import { Conversation, Customer, Webhook } from '@g-tateth/sdk';

const conversation: Conversation = await client.conversations.get('id');
```

## Examples

### Pagination

```typescript theme={null}
async function getAllConversations() {
  let page = 1;
  let allConversations = [];
  
  while (true) {
    const response = await client.conversations.list({ page, limit: 50 });
    allConversations.push(...response.data);
    
    if (page >= response.pagination.totalPages) break;
    page++;
  }
  
  return allConversations;
}
```

### Error Handling

```typescript theme={null}
async function safeGetConversation(id: string) {
  try {
    return await client.conversations.get(id);
  } catch (error) {
    if (error instanceof GtatethError) {
      console.error(`API Error: ${error.code} - ${error.message}`);
    }
    throw error;
  }
}
```

## Resources

* [GitHub Repository](https://github.com/g-tateth/sdk-javascript)
* [npm Package](https://www.npmjs.com/package/@g-tateth/sdk)
* [API Reference](/guides/authentication)
