docs
0
# Client Setup
1
2
Mux client initialization and configuration options for connecting to the Mux API with various authentication methods and request settings.
3
4
## Imports
5
6
```typescript
7
import Mux from "@mux/mux-node";
8
// or
9
import { Mux } from "@mux/mux-node";
10
11
// Additional imports for advanced configuration
12
import { type ClientOptions } from "@mux/mux-node";
13
```
14
15
For CommonJS:
16
17
```javascript
18
const Mux = require("@mux/mux-node");
19
// or
20
const { Mux } = require("@mux/mux-node");
21
```
22
23
## Capabilities
24
25
### Mux Client Constructor
26
27
Creates a new Mux API client with configurable authentication and request options.
28
29
```typescript { .api }
30
/**
31
* API Client for interfacing with the Mux API.
32
* @param options - Configuration options for the client
33
*/
34
constructor(options?: ClientOptions);
35
36
interface ClientOptions {
37
/** Mux token ID (defaults to MUX_TOKEN_ID environment variable) */
38
tokenId?: string | null | undefined;
39
/** Mux token secret (defaults to MUX_TOKEN_SECRET environment variable) */
40
tokenSecret?: string | null | undefined;
41
/** Webhook verification secret (defaults to MUX_WEBHOOK_SECRET environment variable) */
42
webhookSecret?: string | null | undefined;
43
/** JWT signing key (defaults to MUX_SIGNING_KEY environment variable) */
44
jwtSigningKey?: string | null | undefined;
45
/** JWT private key (defaults to MUX_PRIVATE_KEY environment variable) */
46
jwtPrivateKey?: string | null | undefined;
47
/** Authorization token (defaults to MUX_AUTHORIZATION_TOKEN environment variable) */
48
authorizationToken?: string | null | undefined;
49
/** Override the default base URL for the API (defaults to https://api.mux.com) */
50
baseURL?: string | null | undefined;
51
/** Request timeout in milliseconds (defaults to 60000) */
52
timeout?: number | undefined;
53
/** Custom HTTP agent for connection management */
54
httpAgent?: Agent | undefined;
55
/** Custom fetch function implementation */
56
fetch?: Core.Fetch | undefined;
57
/** Maximum number of retry attempts (defaults to 2) */
58
maxRetries?: number | undefined;
59
/** Default headers to include with every request */
60
defaultHeaders?: Core.Headers | undefined;
61
/** Default query parameters to include with every request */
62
defaultQuery?: Core.DefaultQuery | undefined;
63
}
64
```
65
66
**Usage Examples:**
67
68
```typescript
69
import Mux from "@mux/mux-node";
70
71
// Basic initialization with token ID and secret
72
const mux = new Mux({
73
tokenId: "your-token-id",
74
tokenSecret: "your-token-secret",
75
});
76
77
// Environment variable initialization (recommended)
78
const mux = new Mux(); // Uses MUX_TOKEN_ID and MUX_TOKEN_SECRET from env
79
80
// Authorization token authentication
81
const mux = new Mux({
82
authorizationToken: "your-auth-token",
83
});
84
85
// Custom configuration
86
const mux = new Mux({
87
tokenId: process.env.MUX_TOKEN_ID,
88
tokenSecret: process.env.MUX_TOKEN_SECRET,
89
baseURL: "https://api-staging.mux.com",
90
timeout: 30000,
91
maxRetries: 5,
92
defaultHeaders: {
93
"X-Custom-Header": "custom-value",
94
},
95
});
96
```
97
98
### Static Properties
99
100
Access to static constants and utility methods on the Mux class.
101
102
```typescript { .api }
103
/** Default request timeout in milliseconds */
104
static DEFAULT_TIMEOUT: number;
105
106
/** Convert data to File object for uploads */
107
static toFile: typeof toFile;
108
109
/** Create File from file system path */
110
static fileFromPath: typeof fileFromPath;
111
```
112
113
### Error Classes
114
115
Static access to all Mux error classes for error handling and type checking.
116
117
```typescript { .api }
118
static MuxError: typeof MuxError;
119
static APIError: typeof APIError;
120
static APIConnectionError: typeof APIConnectionError;
121
static APIConnectionTimeoutError: typeof APIConnectionTimeoutError;
122
static APIUserAbortError: typeof APIUserAbortError;
123
static NotFoundError: typeof NotFoundError;
124
static ConflictError: typeof ConflictError;
125
static RateLimitError: typeof RateLimitError;
126
static BadRequestError: typeof BadRequestError;
127
static AuthenticationError: typeof AuthenticationError;
128
static InternalServerError: typeof InternalServerError;
129
static PermissionDeniedError: typeof PermissionDeniedError;
130
static UnprocessableEntityError: typeof UnprocessableEntityError;
131
```
132
133
### Resource Access
134
135
Access to all API resources through the initialized client instance.
136
137
```typescript { .api }
138
/** Video operations including assets, live streams, uploads, and playback */
139
video: Video;
140
141
/** Analytics, metrics, and monitoring data */
142
data: Data;
143
144
/** System-level operations including signing keys and utilities */
145
system: System;
146
147
/** Webhook signature verification and event parsing */
148
webhooks: Webhooks;
149
150
/** JWT token signing for secure access */
151
jwt: Jwt;
152
```
153
154
## Authentication Methods
155
156
### Token ID and Secret Authentication
157
158
Primary authentication method using Mux token pairs with automatic Basic authentication header generation.
159
160
```typescript
161
const mux = new Mux({
162
tokenId: "your-token-id",
163
tokenSecret: "your-token-secret",
164
});
165
166
// Or use environment variables
167
// MUX_TOKEN_ID=your-token-id
168
// MUX_TOKEN_SECRET=your-token-secret
169
const mux = new Mux();
170
```
171
172
### Authorization Token Authentication
173
174
Alternative authentication using bearer tokens for simplified authentication workflows.
175
176
```typescript
177
const mux = new Mux({
178
authorizationToken: "your-bearer-token",
179
});
180
181
// Or use environment variable
182
// MUX_AUTHORIZATION_TOKEN=your-bearer-token
183
const mux = new Mux();
184
```
185
186
## Configuration Options
187
188
### Network Configuration
189
190
```typescript
191
const mux = new Mux({
192
// Custom API endpoint
193
baseURL: "https://api-staging.mux.com",
194
195
// Request timeout (milliseconds)
196
timeout: 30000,
197
198
// Retry configuration
199
maxRetries: 3,
200
201
// Custom HTTP agent
202
httpAgent: new Agent({
203
keepAlive: true,
204
maxSockets: 10,
205
}),
206
});
207
```
208
209
### Default Headers and Query Parameters
210
211
```typescript
212
const mux = new Mux({
213
// Headers added to every request
214
defaultHeaders: {
215
"X-Custom-Header": "value",
216
"User-Agent": "MyApp/1.0",
217
},
218
219
// Query parameters added to every request
220
defaultQuery: {
221
"version": "2024-01",
222
},
223
});
224
```
225
226
### Custom Fetch Implementation
227
228
```typescript
229
import fetch from "node-fetch";
230
231
const mux = new Mux({
232
// Custom fetch for specific environments
233
fetch: fetch as any,
234
});
235
```
236
237
## Types
238
239
```typescript { .api }
240
// Core types referenced in ClientOptions
241
namespace Core {
242
/** Custom fetch function interface */
243
export type Fetch = (url: RequestInfo, init?: RequestInit) => Promise<Response>;
244
245
/** HTTP headers record type */
246
export type Headers = Record<string, string | null | undefined>;
247
248
/** Default query parameters type */
249
export type DefaultQuery = Record<string, string | undefined>;
250
251
/** Request configuration options */
252
export interface RequestOptions {
253
timeout?: number;
254
httpAgent?: Agent;
255
signal?: AbortSignal;
256
idempotencyKey?: string;
257
}
258
}
259
260
/** HTTP Agent interface for connection management */
261
interface Agent {
262
keepAlive?: boolean;
263
keepAliveMsecs?: number;
264
maxSockets?: number;
265
maxFreeSockets?: number;
266
timeout?: number;
267
scheduling?: "lifo" | "fifo";
268
}
269
270
/** Environment variables used by the SDK */
271
interface Environment {
272
MUX_TOKEN_ID?: string;
273
MUX_TOKEN_SECRET?: string;
274
MUX_WEBHOOK_SECRET?: string;
275
MUX_SIGNING_KEY?: string;
276
MUX_PRIVATE_KEY?: string;
277
MUX_AUTHORIZATION_TOKEN?: string;
278
MUX_BASE_URL?: string;
279
}
280
```