# TypeScript SDK

## Overview

This SDK provides TypeScript functions for all Mobile Text Alerts API endpoints, and provides:

* **Request/Response Types** - All API data structures are typed
* [**Error Handling** ](#error-handling)- Specific error types for each endpoint
* **Options Types** - Configuration options with proper typing
* **Auto-completion** - Full IDE support with intelligent suggestions

## Using the TypeScript SDK

### Install Node

The Mobile Text Alerts TypeScript SDK requires a working node installation of Node.js and npm. You can check which version of Node you already have installed by running the following command:

```
node -v
```

[Learn how to install or update Node to a newer version.](https://nodejs.org/en/download/)

### Install `@mobiletextalerts/typescript-sdk`&#x20;

The Mobile Text Alerts TypeScript SDK is open-source and hosted on GitHub:

* <a href="https://www.npmjs.com/package/@mobiletextalerts/typescript-sdk" class="button primary" data-icon="npm">NPM package</a>&#x20;
* <a href="https://github.com/mobiletextalerts/typescript-sdk" class="button primary" data-icon="github">GitHub source code</a>&#x20;

The latest version of the TypeScript SDK can be installed with `npm`:

```bash
npm install @mobiletextalerts/typescript-sdk
```

## Client Configuration

The client acts as a single point of configuration for all your API interactions. This is where you set your [API key](/getting-started/get-an-api-key.md) for authentication, the base URL and other options. This is done during initialization, so it does not need to be passed in every individual API call.

### Example client configuration

```typescript
import { createClient, type ClientOptions } from '@mobiletextalerts/typescript-sdk';

const clientOptions: ClientOptions = {
  baseUrl: 'https://api.mobile-text-alerts.com/v3',
  headers: {
    'Authorization': 'Bearer YOUR_API_KEY',
    'Content-Type': 'application/json'
  },
  // Additional client configuration options
};

const client = createClient(clientOptions);
```

## Usage Examples

After creating a client, you can use TypeScript functions to call any of the Mobile Text Alerts API endpoints.

### Send a Message

The following example calls the [Send](/api-reference/send.md#post-send) endpoint:

```typescript
import { sendMessageFromApi } from '@mobiletextalerts/typescript-sdk';

const result = await sendMessageFromApi({
  client,
  body: {
    message: "Hello from your app!",
    groups: [1234567890],
    subscribers: ["1112223333","1222333444"],
    subscriberIds: [187572],
    // Additional message options
  }
});
```

### List Subscribers

The following example calls the [Subscribers](/api-reference/subscribers.md#get-subscribers) endpoint:

```typescript
import { subscribersListSubscribers } from '@mobiletextalerts/typescript-sdk';

const subscribers = await subscribersListSubscribers({
  client,
  query: {
    page: 1,
    pageSize: 100
  }
});
```

### Add a Subscriber

The following example calls the [Subscribers](/api-reference/subscribers.md#post-subscribers) endpoint:

```typescript
import { subscribersCreateSubscriber } from '@mobiletextalerts/typescript-sdk';

const result = await subscribersCreateSubscriber({
  client,
  body: {
     firstName: "FirstName", 
     lastName: "LastName", 
     number: 8002223333, 
     email: "example@mobile-text-alerts.com", 
     // Additional optional subscriber fields
  }
});
```

### Create a Template

The following example calls the [Templates](/api-reference/templates.md#post-templates) endpoint:

```typescript
import { templatesCreateTemplate } from '@mobiletextalerts/typescript-sdk';

const template = await templatesCreateTemplate({
  client,
  body: {
    name: "Welcome Message",
    content: "Welcome to our service, {firstName}!"
  }
});
```

## Error Handling

If the API returns an HTTP [error code](/api-basics/error-response-codes.md) (`400`, `401`, `404`, `500`, etc.), the SDK intercepts the response and transforms it into a meaningful exception class for each endpoint.

```typescript
try {
  const result = await subscribersListSubscribers({ client });
  // Handle success
} catch (error) {
  // Error types are automatically inferred based on the endpoint
  console.error('API Error:', error);
}
```

### Rate Limits

Most Mobile Text Alerts API endpoints have a general rate limit of `30` requests per minute per IP address. However, some individual endpoints may override the general rate limit, see [Rate Limits](/api-basics/rate-limits.md) to learn more.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developers.mobile-text-alerts.com/sdks/typescript-sdk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
