2026-01-28 23:00:58 -05:00

111 lines
2.7 KiB
Markdown

# TextNow Unofficial API
TypeScript client for the TextNow messaging service.
## Installation
```bash
npm install textnow-unofficial-api
```
## Authentication
TextNow uses cookie-based authentication. You need to extract cookies from your browser:
1. Log into TextNow in your browser
2. Open DevTools → Application → Cookies
3. Copy the values for: `connect.sid`, `_csrf`, `XSRF-TOKEN`
4. Get your username from the URL (textnow.com/messaging shows it)
```typescript
import { createTextNowClientWithCredentials } from 'textnow-unofficial-api';
const cookies = 'connect.sid=xxx; _csrf=xxx; XSRF-TOKEN=xxx';
const client = createTextNowClientWithCredentials('your-username', cookies);
```
**Important**: The `XSRF-TOKEN` cookie value is used for the `X-CSRF-Token` header (not `_csrf`).
## Usage
### Send SMS
```typescript
await client.send('+15551234567', 'Hello from TextNow!');
```
### Send Media
```typescript
// From file path
await client.sendImage('+15551234567', '/path/to/image.jpg');
// From buffer
await client.sendImageBuffer('+15551234567', buffer, 'photo.jpg', 'image/jpeg');
// Audio
await client.sendAudio('+15551234567', '/path/to/audio.mp3');
```
### Get Messages
```typescript
// All recent messages
const { messages } = await client.getMessages({ limit: 50 });
// From specific contact
const msgs = await client.getMessagesFrom('+15551234567');
```
### Get Conversations
```typescript
const { conversations } = await client.getConversations();
```
## Credential Storage
Credentials are stored in `~/.textnow/credentials.json` with restricted permissions.
```typescript
import {
loadCredentials,
hasStoredCredentials,
clearCredentials
} from 'textnow-unofficial-api';
// Check if credentials exist
if (hasStoredCredentials()) {
const creds = loadCredentials();
console.log(`Logged in as: ${creds.username}`);
}
// Clear stored credentials
clearCredentials();
```
## API Reference
### TextNowAPI
- `send(to: string, message: string)` - Send SMS
- `sendMedia(options: SendMediaRequest)` - Send media attachment
- `sendImage(to: string, filePath: string)` - Send image file
- `sendImageBuffer(to: string, buffer: Buffer, filename?, contentType?)` - Send image from buffer
- `sendAudio(to: string, filePath: string)` - Send audio file
- `getMessages(params?)` - Get messages
- `getMessagesFrom(contactNumber: string, limit?)` - Get messages from contact
- `getConversations(params?)` - Get conversations list
- `getUploadUrl(mediaType?)` - Get presigned upload URL
### TextNowAuth
- `setCredentials(username, cookies, save?)` - Set auth credentials
- `getAuthHeaders()` - Get headers for API requests
- `isAuthenticated()` - Check auth status
- `logout(clearStored?)` - Clear auth state
## License
MIT