@unkey/api 0.26.1 → 0.27.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +118 -0
- package/dist/index.d.mts +496 -13
- package/dist/index.d.ts +496 -13
- package/dist/index.js +33 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +33 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -3
package/README.md
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<h1 align="center">@unkey/api</h1>
|
|
3
|
+
<h5>`@unkey/api` is a TypeScript client for Unkey. If you prefer a typed experience over calling HTTP endpoints directly, this SDK is for you.</h5>
|
|
4
|
+
</div>
|
|
5
|
+
|
|
6
|
+
<div align="center">
|
|
7
|
+
<a href="https://www.unkey.com/docs/libraries/ts/sdk/overview">Documentation</a>
|
|
8
|
+
</div>
|
|
9
|
+
<br/>
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @unkey/api
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Quickstart
|
|
18
|
+
|
|
19
|
+
1. Create a new Unkey Root Key in the settings.
|
|
20
|
+
2. Use the root key to initialize the client:
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { Unkey } from "@unkey/api";
|
|
24
|
+
|
|
25
|
+
const unkey = new Unkey({ rootKey: "<UNKEY_ROOT_KEY>" });
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
**Important:** Always keep your root key safe and reset it if you suspect it has been compromised.
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
### Verifying a Key
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { verifyKey } from "@unkey/api";
|
|
36
|
+
|
|
37
|
+
const { result, error } = await verifyKey("key_123");
|
|
38
|
+
|
|
39
|
+
if (error) {
|
|
40
|
+
console.error(error.message);
|
|
41
|
+
// Handle potential network or bad request error
|
|
42
|
+
// A link to our docs will be in the `error.docs` field
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (!result.valid) {
|
|
47
|
+
// Do not grant access
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Process request
|
|
52
|
+
console.log(result);
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Response Format
|
|
56
|
+
|
|
57
|
+
All methods return either an `error` or a `result` field, never both and never none. This approach helps with proper error handling.
|
|
58
|
+
|
|
59
|
+
### Success Response
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
{
|
|
63
|
+
result: T; // The result depends on what method you called
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Error Response
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
{
|
|
71
|
+
error: {
|
|
72
|
+
message: string;
|
|
73
|
+
docs: string; // URL to relevant documentation
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Configuration Options
|
|
79
|
+
|
|
80
|
+
### Base URL
|
|
81
|
+
|
|
82
|
+
You can customize the base URL for all requests:
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
const unkey = new Unkey({
|
|
86
|
+
rootKey: "<UNKEY_ROOT_KEY>",
|
|
87
|
+
baseUrl: "https://my.domain",
|
|
88
|
+
});
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Retries
|
|
92
|
+
|
|
93
|
+
Configure retry behavior for network errors:
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
const unkey = new Unkey({
|
|
97
|
+
rootKey: "<UNKEY_ROOT_KEY>",
|
|
98
|
+
retry: {
|
|
99
|
+
attempts: 3,
|
|
100
|
+
backoff: (retryCount) => retryCount * 1000,
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Disable Telemetry
|
|
106
|
+
|
|
107
|
+
To opt out of anonymous telemetry data collection:
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
const unkey = new Unkey({
|
|
111
|
+
rootKey: "<UNKEY_ROOT_KEY>",
|
|
112
|
+
disableTelemetry: true,
|
|
113
|
+
});
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Documentation
|
|
117
|
+
|
|
118
|
+
[Read the full documentation](https://www.unkey.com/docs/libraries/ts/sdk/overview)
|