0
# Configuration and Authentication
1
2
Configuration settings for the Tinify client including API key authentication, proxy settings, and application identification.
3
4
## Capabilities
5
6
### API Key Authentication
7
8
Set your Tinify API key for authentication. This is required before using any image processing functionality.
9
10
```typescript { .api }
11
/**
12
* Set the API key for authentication with the Tinify API
13
* @example tinify.key = "your-api-key-here"
14
*/
15
let key: string;
16
```
17
18
**Usage Example:**
19
20
```typescript
21
const tinify = require("tinify");
22
23
// Set API key (required)
24
tinify.key = "your-api-key-here";
25
26
// Now you can use image processing methods
27
await tinify.fromFile("image.png").toFile("compressed.png");
28
```
29
30
### Application Identifier
31
32
Set an application identifier to be included in the user agent string for API requests.
33
34
```typescript { .api }
35
/**
36
* Set application identifier for user agent string
37
* @example tinify.appIdentifier = "MyApp/1.0"
38
*/
39
let appIdentifier: string;
40
```
41
42
**Usage Example:**
43
44
```typescript
45
const tinify = require("tinify");
46
47
tinify.key = "your-api-key";
48
tinify.appIdentifier = "MyApp/1.0";
49
// User agent will include: "Tinify/1.8.1 Node/v14.15.0 (linux) MyApp/1.0"
50
```
51
52
### Proxy Configuration
53
54
Configure proxy settings for API requests when operating behind a corporate proxy.
55
56
```typescript { .api }
57
/**
58
* Set proxy URL for API requests
59
* @example tinify.proxy = "http://proxy.example.com:8080"
60
*/
61
let proxy: string;
62
```
63
64
**Usage Example:**
65
66
```typescript
67
const tinify = require("tinify");
68
69
tinify.key = "your-api-key";
70
tinify.proxy = "http://proxy.example.com:8080";
71
```
72
73
### Compression Count
74
75
Read-only property that tracks the number of compressions made with your API key this month.
76
77
```typescript { .api }
78
/**
79
* Current compression count from API headers (read-only)
80
* Updated automatically after each API request
81
*/
82
let compressionCount?: number;
83
```
84
85
**Usage Example:**
86
87
```typescript
88
const tinify = require("tinify");
89
90
tinify.key = "your-api-key";
91
92
await tinify.fromFile("image.png").toFile("compressed.png");
93
94
console.log(`Compressions used: ${tinify.compressionCount}`);
95
```
96
97
### API Key Validation
98
99
Validate your API key without performing actual image compression.
100
101
```typescript { .api }
102
/**
103
* Validate API key without performing compression
104
* @returns Promise that resolves if key is valid, rejects on error
105
*/
106
function validate(): Promise<void>;
107
108
/**
109
* Validate API key with callback support
110
* @param callback - Callback function receiving error or null
111
*/
112
function validate(callback: (err: Error | null) => void): void;
113
```
114
115
**Usage Examples:**
116
117
```typescript
118
const tinify = require("tinify");
119
120
tinify.key = "your-api-key";
121
122
// Promise-based validation
123
try {
124
await tinify.validate();
125
console.log("API key is valid");
126
} catch (error) {
127
console.error("Invalid API key:", error.message);
128
}
129
130
// Callback-based validation
131
tinify.validate((err) => {
132
if (err) {
133
console.error("Invalid API key:", err.message);
134
} else {
135
console.log("API key is valid");
136
}
137
});
138
```
139
140
## Error Handling
141
142
Configuration methods can throw the following errors:
143
144
- **AccountError**: Invalid API key, exceeded monthly limit, or billing issues
145
- **ConnectionError**: Network connectivity problems
146
- **ClientError**: Invalid proxy URL or malformed requests