theauthapi 1.0.15 → 1.0.16
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 +366 -366
- package/dist/endpoints/Accounts/Accounts.d.ts +3 -3
- package/dist/endpoints/Accounts/Accounts.js +2 -2
- package/dist/endpoints/Accounts/AccountsInterface.d.ts +1 -1
- package/dist/endpoints/ApiKeys/ApiKeys.d.ts +3 -3
- package/dist/endpoints/ApiKeys/ApiKeys.js +5 -5
- package/dist/endpoints/ApiKeys/ApiKeysInterface.d.ts +1 -1
- package/dist/endpoints/Projects/Projects.d.ts +3 -3
- package/dist/endpoints/Projects/Projects.js +2 -2
- package/dist/endpoints/Projects/ProjectsInterface.d.ts +1 -1
- package/dist/index.cjs +15 -15
- package/dist/index.d.cts +200 -0
- package/dist/index.d.mts +200 -0
- package/dist/index.d.ts +6 -180
- package/dist/index.js +9 -9
- package/dist/index.mjs +15 -15
- package/dist/libraryMeta.js +1 -1
- package/dist/services/ApiRequest/ApiCall.d.ts +1 -1
- package/dist/services/ApiRequest/ApiRequest.d.ts +5 -5
- package/dist/services/ApiRequest/ApiRequest.js +11 -11
- package/dist/services/ApiRequest/ApiResponseError.js +1 -1
- package/package.json +23 -6
package/README.md
CHANGED
|
@@ -1,366 +1,366 @@
|
|
|
1
|
-
# Client library for [TheAuthAPI](https://theauthapi.com/)
|
|
2
|
-
|
|
3
|
-
**Contents**
|
|
4
|
-
|
|
5
|
-
- [Client library for TheAuthAPI](#client-library-for-theauthapi)
|
|
6
|
-
- [Installation](#installation)
|
|
7
|
-
- [Configuration](#configuration)
|
|
8
|
-
- [Imports](#imports)
|
|
9
|
-
- [CommonJS](#commonjs)
|
|
10
|
-
- [ES Modules](#es-modules)
|
|
11
|
-
- [Usage](#usage)
|
|
12
|
-
- [Example: Validating an API-Key](#example-validating-an-api-key)
|
|
13
|
-
- [Example: Listing API-keys](#example-listing-api-keys)
|
|
14
|
-
- [Example: Listing the projects of an account](#example-listing-the-projects-of-an-account)
|
|
15
|
-
- [Example: Listing projects and associated API-Keys](#example-listing-projects-and-associated-api-keys)
|
|
16
|
-
- [Example: Creating an API-Key](#example-creating-an-api-key)
|
|
17
|
-
- [Example: Rotating an API-Key](#example-rotating-an-api-key)
|
|
18
|
-
- [Handling Errors](#handling-errors)
|
|
19
|
-
- [ApiRequestError](#apirequesterror)
|
|
20
|
-
- [ApiResponseError](#apiresponseerror)
|
|
21
|
-
- [Example: Getting a key throws an ApiResponseError if the key is invalid](#example-getting-a-key-throws-an-apiresponseerror-if-the-key-is-invalid)
|
|
22
|
-
- [Error](#error)
|
|
23
|
-
- [Handling Errors the Right Way](#handling-errors-the-right-way)
|
|
24
|
-
- [Typescript](#typescript)
|
|
25
|
-
- [📙 Further Reading](#-further-reading)
|
|
26
|
-
|
|
27
|
-
**Scalable API Key Management and Auth Control**
|
|
28
|
-
Secure your API with best in class API Key management, user access, all with great analytics.
|
|
29
|
-
|
|
30
|
-
## Installation
|
|
31
|
-
|
|
32
|
-
This library is published on [npm](https://www.npmjs.com/package/theauthapi), you can add it as a dependency using the following
|
|
33
|
-
command
|
|
34
|
-
|
|
35
|
-
```bash
|
|
36
|
-
npm install theauthapi
|
|
37
|
-
# or
|
|
38
|
-
yarn add theauthapi
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
## Configuration
|
|
42
|
-
|
|
43
|
-
You'll need to configure the library with your `access key` and `account id`, you can grab these from [TheAuthAPI](https://app.theauthapi.com) dashboard.
|
|
44
|
-
|
|
45
|
-
For further instructions on creating an account, check out our [how to guides](https://support.theauthapi.com/).
|
|
46
|
-
|
|
47
|
-
### Imports
|
|
48
|
-
|
|
49
|
-
#### CommonJS
|
|
50
|
-
|
|
51
|
-
```javascript
|
|
52
|
-
const TheAuthAPI = require(
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
#### ES Modules
|
|
56
|
-
|
|
57
|
-
```typescript
|
|
58
|
-
import TheAuthAPI from
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
initialize the client using your access key:
|
|
62
|
-
|
|
63
|
-
```javascript
|
|
64
|
-
const theAuthAPI = new TheAuthAPI(
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
You can also provide custom options:
|
|
68
|
-
|
|
69
|
-
```javascript
|
|
70
|
-
const theAuthAPI = new TheAuthAPI(
|
|
71
|
-
retryCount: 2,
|
|
72
|
-
});
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
**Full option types:**
|
|
76
|
-
|
|
77
|
-
```typescript
|
|
78
|
-
type Options = {
|
|
79
|
-
// server url
|
|
80
|
-
host?: string;
|
|
81
|
-
// number of retries before failing
|
|
82
|
-
retryCount?: number;
|
|
83
|
-
};
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
## Usage
|
|
87
|
-
|
|
88
|
-
After initiating the client, you can access endpoint methods using the following pattern:
|
|
89
|
-
`[object instance].[endpoint].[method]`
|
|
90
|
-
|
|
91
|
-
For example, getting the projects for an account would be: `theAuthApiClient.projects.getProjects("ACCOUNT_ID")`,
|
|
92
|
-
|
|
93
|
-
Similarly, getting the api keys would be:
|
|
94
|
-
`theAuthApiClient.apiKeys.getKeys("PROJECT_ID")`
|
|
95
|
-
|
|
96
|
-
| endpoint | attribute | example |
|
|
97
|
-
| --------- | --------- | --------------------------------------------- |
|
|
98
|
-
| /api-keys | apiKeys | `client.apiKeys.createKey("MY_KEY")` |
|
|
99
|
-
| /projects | projects | `client.projects.createProject("MY_PROJECT")` |
|
|
100
|
-
| /accounts | accounts | `client.accounts.createAccount("MY_ACCOUNT")` |
|
|
101
|
-
|
|
102
|
-
For details on each endpoint accepted values, please reference these docs: [docs.theauthapi.com](https://docs.theauthapi.com/)
|
|
103
|
-
|
|
104
|
-
All methods return a promise containing the returned JSON as a javascript object. Each method of an endpoint maps HTTP methods to
|
|
105
|
-
|
|
106
|
-
| HTTP Method | method name | example |
|
|
107
|
-
| ----------- | ----------- | ------------------------------------------------------------------------- |
|
|
108
|
-
| POST | create\* | `client.apiKeys.createKey({ name: "KEY_NAME", projectId: "PROJECT_ID" })` |
|
|
109
|
-
| GET | get\* | `client.apiKeys.getKeys()` |
|
|
110
|
-
| DELETE | delete\* | `client.apiKeys.deleteKey("MY_KEY")` |
|
|
111
|
-
| PATCH | update\* | `client.apiKeys.updateKey("MY_KEY", { name: "UPDATED_KEY_NAME" })` |
|
|
112
|
-
|
|
113
|
-
#### Example: Validating an API-Key
|
|
114
|
-
|
|
115
|
-
You can easily validate an API key using `apiKeys.isValidKey` which returns `true` if the key is valid, `false` otherwise.
|
|
116
|
-
`isValidKey` throws an `ApiRequestError` if there's a network issue, it's advised to wrap it in a `try/catch` to handle the potential error
|
|
117
|
-
|
|
118
|
-
```javascript
|
|
119
|
-
theAuthAPI.apiKeys
|
|
120
|
-
.isValidKey(
|
|
121
|
-
.then((isValidKey) => {
|
|
122
|
-
if (isValidKey) {
|
|
123
|
-
console.log(
|
|
124
|
-
} else {
|
|
125
|
-
console.log(
|
|
126
|
-
}
|
|
127
|
-
})
|
|
128
|
-
.catch((error) => {
|
|
129
|
-
// handle network error
|
|
130
|
-
});
|
|
131
|
-
```
|
|
132
|
-
|
|
133
|
-
**Using async/await**
|
|
134
|
-
|
|
135
|
-
```javascript
|
|
136
|
-
try {
|
|
137
|
-
const isValidKey = await theAuthAPI.apiKeys.isValidKey(
|
|
138
|
-
if (isValidKey) {
|
|
139
|
-
console.log(
|
|
140
|
-
} else {
|
|
141
|
-
console.log(
|
|
142
|
-
}
|
|
143
|
-
} catch (error) {
|
|
144
|
-
// handle network error
|
|
145
|
-
}
|
|
146
|
-
```
|
|
147
|
-
|
|
148
|
-
**Note:** If you want to consume the API key and get the API key entity in return, you can use `apiKeys.authenticateKey` which returns an ApiKey.
|
|
149
|
-
|
|
150
|
-
#### Example: Listing API-keys
|
|
151
|
-
|
|
152
|
-
```javascript
|
|
153
|
-
theAuthAPI.apiKeys
|
|
154
|
-
.getKeys()
|
|
155
|
-
.then((keys) => console.log(keys))
|
|
156
|
-
.catch((error) => console.log(error));
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
**Using async/await**
|
|
160
|
-
|
|
161
|
-
```javascript
|
|
162
|
-
try {
|
|
163
|
-
const keys = await theAuthAPI.apiKeys.getKeys();
|
|
164
|
-
} catch (error) {
|
|
165
|
-
console.log(error);
|
|
166
|
-
}
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
**Filtering API Keys**: You can filter the listed API keys by passing an object of type filter as an argument to `getKeys`
|
|
170
|
-
|
|
171
|
-
```typescript
|
|
172
|
-
type ApiKeyFilter = {
|
|
173
|
-
projectId?: string;
|
|
174
|
-
name?: string;
|
|
175
|
-
customAccountId?: string | null;
|
|
176
|
-
customUserId?: string | null;
|
|
177
|
-
isActive?: boolean;
|
|
178
|
-
};
|
|
179
|
-
```
|
|
180
|
-
|
|
181
|
-
**Example**: filtering api-keys with a specific `projectId` where the keys are not active
|
|
182
|
-
|
|
183
|
-
```javascript
|
|
184
|
-
try {
|
|
185
|
-
const keys = await theAuthAPI.apiKeys.getKeys({
|
|
186
|
-
projectId:
|
|
187
|
-
isActive: false,
|
|
188
|
-
});
|
|
189
|
-
} catch (error) {
|
|
190
|
-
console.log(error);
|
|
191
|
-
}
|
|
192
|
-
```
|
|
193
|
-
|
|
194
|
-
**NOTE** that if your access key is at account level, you need to specify `projectId` when listing the API keys:
|
|
195
|
-
`getKeys({ projectId: "PROJECT_ID" })`, otherwise if your access key is created at project level, you don't have to specify `projectId`,
|
|
196
|
-
the access key's `projectId` will be used to get the API-keys (i.e. you'll see only the keys of the project your access key is created against)
|
|
197
|
-
|
|
198
|
-
#### Example: Listing the projects of an account
|
|
199
|
-
|
|
200
|
-
```javascript
|
|
201
|
-
theAuthAPI.projects
|
|
202
|
-
.getProjects(
|
|
203
|
-
.then((projects) => console.log(projects))
|
|
204
|
-
.catch((error) => console.log(error));
|
|
205
|
-
```
|
|
206
|
-
|
|
207
|
-
**Using async/await**
|
|
208
|
-
|
|
209
|
-
```javascript
|
|
210
|
-
try {
|
|
211
|
-
const projects = await client.projects.getProjects(
|
|
212
|
-
} catch (error) {
|
|
213
|
-
console.log(error);
|
|
214
|
-
}
|
|
215
|
-
```
|
|
216
|
-
|
|
217
|
-
#### Example: Listing projects and associated API-Keys
|
|
218
|
-
|
|
219
|
-
```javascript
|
|
220
|
-
async function getProjectsWithKeys(accountId: string) {
|
|
221
|
-
try {
|
|
222
|
-
const projects = await theAuthAPI.projects.getProjects(accountId);
|
|
223
|
-
const projectsKeys = projects.map(async (project) => {
|
|
224
|
-
const keys = await theAuthAPI.apiKeys.getKeys(project.id);
|
|
225
|
-
return { project, keys };
|
|
226
|
-
});
|
|
227
|
-
return await Promise.all(projectsKeys);
|
|
228
|
-
} catch (error) {
|
|
229
|
-
// handle error
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
```
|
|
233
|
-
|
|
234
|
-
#### Example: Creating an API-Key
|
|
235
|
-
|
|
236
|
-
```javascript
|
|
237
|
-
theAuthAPI.apiKeys
|
|
238
|
-
.createKey({
|
|
239
|
-
projectId:
|
|
240
|
-
customMetaData: { metadata_val:
|
|
241
|
-
customAccountId:
|
|
242
|
-
name:
|
|
243
|
-
})
|
|
244
|
-
.then((key) => console.log(
|
|
245
|
-
.catch((error) => console.log("Couldn't make the key", error));
|
|
246
|
-
```
|
|
247
|
-
|
|
248
|
-
**Using async/await**
|
|
249
|
-
|
|
250
|
-
```javascript
|
|
251
|
-
try {
|
|
252
|
-
const key = await theAuthAPI.apiKeys.createKey({
|
|
253
|
-
projectId:
|
|
254
|
-
customMetaData: { metadata_val:
|
|
255
|
-
customAccountId:
|
|
256
|
-
name:
|
|
257
|
-
});
|
|
258
|
-
console.log(
|
|
259
|
-
} catch (error) {
|
|
260
|
-
console.log("Couldn't make the key ", error);
|
|
261
|
-
}
|
|
262
|
-
```
|
|
263
|
-
|
|
264
|
-
#### Example: Rotating an API-Key
|
|
265
|
-
|
|
266
|
-
When you need to quickly and securely rotate a compromised key, while preserving the key's metadata, use the `rotateKey` method. This method while clone your key and return you with a new one.
|
|
267
|
-
|
|
268
|
-
```javascript
|
|
269
|
-
theAuthAPI.apiKeys
|
|
270
|
-
.rotateKey(
|
|
271
|
-
.then((key) => console.log(
|
|
272
|
-
.catch((error) => console.log("Couldn't rotate the key", error));
|
|
273
|
-
```
|
|
274
|
-
|
|
275
|
-
**Using async/await**
|
|
276
|
-
|
|
277
|
-
```javascript
|
|
278
|
-
try {
|
|
279
|
-
const key = await theAuthAPI.apiKeys.createKey(
|
|
280
|
-
console.log(
|
|
281
|
-
} catch (error) {
|
|
282
|
-
console.log("Couldn't rotate the key", error);
|
|
283
|
-
}
|
|
284
|
-
```
|
|
285
|
-
|
|
286
|
-
**NOTE** In the background, this marks the old key as inactive and issues you with a new key. Any requests to the old key will be instantly blocked.
|
|
287
|
-
|
|
288
|
-
### Handling Errors
|
|
289
|
-
|
|
290
|
-
[comment]: <> (All methods that return a promise throw 3 types of errors)
|
|
291
|
-
|
|
292
|
-
##### ApiRequestError
|
|
293
|
-
|
|
294
|
-
Thrown when there's a network or a connectivity issue, for example, if the client didn't establish any network connection with the host
|
|
295
|
-
|
|
296
|
-
```
|
|
297
|
-
ApiRequestError: getaddrinfo EAI_AGAIN api.theauthapi.com
|
|
298
|
-
```
|
|
299
|
-
|
|
300
|
-
##### ApiResponseError
|
|
301
|
-
|
|
302
|
-
Thrown when the server responds with an HTTP status code not in the `2xx` range. `ApiRequestError` provides two properties to distinguish the type of the error
|
|
303
|
-
|
|
304
|
-
- `statusCode` HTTP status code
|
|
305
|
-
- `message` the message the server responded with in the body
|
|
306
|
-
|
|
307
|
-
This is the most common thrown error, you should expect and handle it each time you use any of the library methods
|
|
308
|
-
|
|
309
|
-
##### Example: Getting a key throws an ApiResponseError if the key is invalid
|
|
310
|
-
|
|
311
|
-
If you try to GET an invalid key using `apiKeys.getKey("invalid-key")`, the server responds with a 404 error and an `ApiResponseError` is thrown
|
|
312
|
-
|
|
313
|
-
```
|
|
314
|
-
ApiResponseError: (404): Invalid client key
|
|
315
|
-
```
|
|
316
|
-
|
|
317
|
-
"404" is the `statusCode`, "Invalid client Key" is the `message`, you can access these properties using `error.statusCode` and `error.message` respectively
|
|
318
|
-
|
|
319
|
-
##### Error
|
|
320
|
-
|
|
321
|
-
Unknown error, just a normal javascript error
|
|
322
|
-
|
|
323
|
-
#### Handling Errors the Right Way
|
|
324
|
-
|
|
325
|
-
Since all the possible thrown errors are instances of classes, we can check the type of the thrown error and handle it accordingly
|
|
326
|
-
|
|
327
|
-
```javascript
|
|
328
|
-
try {
|
|
329
|
-
const key = await theAuthAPI.apiKeys.getKey(
|
|
330
|
-
} catch (error) {
|
|
331
|
-
if (error instanceof ApiResponseError) {
|
|
332
|
-
// handle response error
|
|
333
|
-
}
|
|
334
|
-
if (error instanceof ApiRequestError) {
|
|
335
|
-
// handle network error
|
|
336
|
-
}
|
|
337
|
-
// unknown error
|
|
338
|
-
throw error;
|
|
339
|
-
}
|
|
340
|
-
```
|
|
341
|
-
|
|
342
|
-
### Typescript
|
|
343
|
-
|
|
344
|
-
This library is written in [Typescript](https://www.typescriptlang.org/), types are provided out of the box.
|
|
345
|
-
|
|
346
|
-
Example of usage with Typescript:
|
|
347
|
-
|
|
348
|
-
```typescript
|
|
349
|
-
import TheAuthAPI from
|
|
350
|
-
import { Project } from
|
|
351
|
-
|
|
352
|
-
const client = new TheAuthAPI(
|
|
353
|
-
|
|
354
|
-
async function getProjectsIds(accountId: string): Promise<string[]> {
|
|
355
|
-
const projects: Project[] = await client.projects.getProjects(accountId);
|
|
356
|
-
return projects.map((project) => project.name);
|
|
357
|
-
}
|
|
358
|
-
```
|
|
359
|
-
|
|
360
|
-
### 📙 Further Reading
|
|
361
|
-
|
|
362
|
-
- Create your account [https://theauthapi.com](https://theauthapi.com)
|
|
363
|
-
- View our [Knowledge Base](https://thatapicompany.notion.site/The-Auth-API-Knowledge-Base-21660cee84e640729714fad43d9ce546) help centre
|
|
364
|
-
- Read our [API docs](https://docs.theauthapi.com)
|
|
365
|
-
- Articles on best Auth practice - [https://theauthapi.com/articles](https://theauthapi.com/articles)
|
|
366
|
-
- Meet the team behind The Auth API - [That API Company](https://thatapicompany.com/)
|
|
1
|
+
# Client library for [TheAuthAPI](https://theauthapi.com/)
|
|
2
|
+
|
|
3
|
+
**Contents**
|
|
4
|
+
|
|
5
|
+
- [Client library for TheAuthAPI](#client-library-for-theauthapi)
|
|
6
|
+
- [Installation](#installation)
|
|
7
|
+
- [Configuration](#configuration)
|
|
8
|
+
- [Imports](#imports)
|
|
9
|
+
- [CommonJS](#commonjs)
|
|
10
|
+
- [ES Modules](#es-modules)
|
|
11
|
+
- [Usage](#usage)
|
|
12
|
+
- [Example: Validating an API-Key](#example-validating-an-api-key)
|
|
13
|
+
- [Example: Listing API-keys](#example-listing-api-keys)
|
|
14
|
+
- [Example: Listing the projects of an account](#example-listing-the-projects-of-an-account)
|
|
15
|
+
- [Example: Listing projects and associated API-Keys](#example-listing-projects-and-associated-api-keys)
|
|
16
|
+
- [Example: Creating an API-Key](#example-creating-an-api-key)
|
|
17
|
+
- [Example: Rotating an API-Key](#example-rotating-an-api-key)
|
|
18
|
+
- [Handling Errors](#handling-errors)
|
|
19
|
+
- [ApiRequestError](#apirequesterror)
|
|
20
|
+
- [ApiResponseError](#apiresponseerror)
|
|
21
|
+
- [Example: Getting a key throws an ApiResponseError if the key is invalid](#example-getting-a-key-throws-an-apiresponseerror-if-the-key-is-invalid)
|
|
22
|
+
- [Error](#error)
|
|
23
|
+
- [Handling Errors the Right Way](#handling-errors-the-right-way)
|
|
24
|
+
- [Typescript](#typescript)
|
|
25
|
+
- [📙 Further Reading](#-further-reading)
|
|
26
|
+
|
|
27
|
+
**Scalable API Key Management and Auth Control**
|
|
28
|
+
Secure your API with best in class API Key management, user access, all with great analytics.
|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
|
|
32
|
+
This library is published on [npm](https://www.npmjs.com/package/theauthapi), you can add it as a dependency using the following
|
|
33
|
+
command
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
npm install theauthapi
|
|
37
|
+
# or
|
|
38
|
+
yarn add theauthapi
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Configuration
|
|
42
|
+
|
|
43
|
+
You'll need to configure the library with your `access key` and `account id`, you can grab these from [TheAuthAPI](https://app.theauthapi.com) dashboard.
|
|
44
|
+
|
|
45
|
+
For further instructions on creating an account, check out our [how to guides](https://support.theauthapi.com/).
|
|
46
|
+
|
|
47
|
+
### Imports
|
|
48
|
+
|
|
49
|
+
#### CommonJS
|
|
50
|
+
|
|
51
|
+
```javascript
|
|
52
|
+
const TheAuthAPI = require('theauthapi');
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
#### ES Modules
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import TheAuthAPI from 'theauthapi';
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
initialize the client using your access key:
|
|
62
|
+
|
|
63
|
+
```javascript
|
|
64
|
+
const theAuthAPI = new TheAuthAPI('YOUR_ACCESS_KEY');
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
You can also provide custom options:
|
|
68
|
+
|
|
69
|
+
```javascript
|
|
70
|
+
const theAuthAPI = new TheAuthAPI('YOUR_ACCESS_KEY', {
|
|
71
|
+
retryCount: 2,
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
**Full option types:**
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
type Options = {
|
|
79
|
+
// server url
|
|
80
|
+
host?: string;
|
|
81
|
+
// number of retries before failing
|
|
82
|
+
retryCount?: number;
|
|
83
|
+
};
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Usage
|
|
87
|
+
|
|
88
|
+
After initiating the client, you can access endpoint methods using the following pattern:
|
|
89
|
+
`[object instance].[endpoint].[method]`
|
|
90
|
+
|
|
91
|
+
For example, getting the projects for an account would be: `theAuthApiClient.projects.getProjects("ACCOUNT_ID")`,
|
|
92
|
+
|
|
93
|
+
Similarly, getting the api keys would be:
|
|
94
|
+
`theAuthApiClient.apiKeys.getKeys("PROJECT_ID")`
|
|
95
|
+
|
|
96
|
+
| endpoint | attribute | example |
|
|
97
|
+
| --------- | --------- | --------------------------------------------- |
|
|
98
|
+
| /api-keys | apiKeys | `client.apiKeys.createKey("MY_KEY")` |
|
|
99
|
+
| /projects | projects | `client.projects.createProject("MY_PROJECT")` |
|
|
100
|
+
| /accounts | accounts | `client.accounts.createAccount("MY_ACCOUNT")` |
|
|
101
|
+
|
|
102
|
+
For details on each endpoint accepted values, please reference these docs: [docs.theauthapi.com](https://docs.theauthapi.com/)
|
|
103
|
+
|
|
104
|
+
All methods return a promise containing the returned JSON as a javascript object. Each method of an endpoint maps HTTP methods to
|
|
105
|
+
|
|
106
|
+
| HTTP Method | method name | example |
|
|
107
|
+
| ----------- | ----------- | ------------------------------------------------------------------------- |
|
|
108
|
+
| POST | create\* | `client.apiKeys.createKey({ name: "KEY_NAME", projectId: "PROJECT_ID" })` |
|
|
109
|
+
| GET | get\* | `client.apiKeys.getKeys()` |
|
|
110
|
+
| DELETE | delete\* | `client.apiKeys.deleteKey("MY_KEY")` |
|
|
111
|
+
| PATCH | update\* | `client.apiKeys.updateKey("MY_KEY", { name: "UPDATED_KEY_NAME" })` |
|
|
112
|
+
|
|
113
|
+
#### Example: Validating an API-Key
|
|
114
|
+
|
|
115
|
+
You can easily validate an API key using `apiKeys.isValidKey` which returns `true` if the key is valid, `false` otherwise.
|
|
116
|
+
`isValidKey` throws an `ApiRequestError` if there's a network issue, it's advised to wrap it in a `try/catch` to handle the potential error
|
|
117
|
+
|
|
118
|
+
```javascript
|
|
119
|
+
theAuthAPI.apiKeys
|
|
120
|
+
.isValidKey('API_KEY')
|
|
121
|
+
.then((isValidKey) => {
|
|
122
|
+
if (isValidKey) {
|
|
123
|
+
console.log('The API is valid!');
|
|
124
|
+
} else {
|
|
125
|
+
console.log('Invalid API key!');
|
|
126
|
+
}
|
|
127
|
+
})
|
|
128
|
+
.catch((error) => {
|
|
129
|
+
// handle network error
|
|
130
|
+
});
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
**Using async/await**
|
|
134
|
+
|
|
135
|
+
```javascript
|
|
136
|
+
try {
|
|
137
|
+
const isValidKey = await theAuthAPI.apiKeys.isValidKey('API_KEY');
|
|
138
|
+
if (isValidKey) {
|
|
139
|
+
console.log('The API is valid!');
|
|
140
|
+
} else {
|
|
141
|
+
console.log('Invalid API key!');
|
|
142
|
+
}
|
|
143
|
+
} catch (error) {
|
|
144
|
+
// handle network error
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
**Note:** If you want to consume the API key and get the API key entity in return, you can use `apiKeys.authenticateKey` which returns an ApiKey.
|
|
149
|
+
|
|
150
|
+
#### Example: Listing API-keys
|
|
151
|
+
|
|
152
|
+
```javascript
|
|
153
|
+
theAuthAPI.apiKeys
|
|
154
|
+
.getKeys()
|
|
155
|
+
.then((keys) => console.log(keys))
|
|
156
|
+
.catch((error) => console.log(error));
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
**Using async/await**
|
|
160
|
+
|
|
161
|
+
```javascript
|
|
162
|
+
try {
|
|
163
|
+
const keys = await theAuthAPI.apiKeys.getKeys();
|
|
164
|
+
} catch (error) {
|
|
165
|
+
console.log(error);
|
|
166
|
+
}
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
**Filtering API Keys**: You can filter the listed API keys by passing an object of type filter as an argument to `getKeys`
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
type ApiKeyFilter = {
|
|
173
|
+
projectId?: string;
|
|
174
|
+
name?: string;
|
|
175
|
+
customAccountId?: string | null;
|
|
176
|
+
customUserId?: string | null;
|
|
177
|
+
isActive?: boolean;
|
|
178
|
+
};
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
**Example**: filtering api-keys with a specific `projectId` where the keys are not active
|
|
182
|
+
|
|
183
|
+
```javascript
|
|
184
|
+
try {
|
|
185
|
+
const keys = await theAuthAPI.apiKeys.getKeys({
|
|
186
|
+
projectId: 'PROJECT_ID',
|
|
187
|
+
isActive: false,
|
|
188
|
+
});
|
|
189
|
+
} catch (error) {
|
|
190
|
+
console.log(error);
|
|
191
|
+
}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
**NOTE** that if your access key is at account level, you need to specify `projectId` when listing the API keys:
|
|
195
|
+
`getKeys({ projectId: "PROJECT_ID" })`, otherwise if your access key is created at project level, you don't have to specify `projectId`,
|
|
196
|
+
the access key's `projectId` will be used to get the API-keys (i.e. you'll see only the keys of the project your access key is created against)
|
|
197
|
+
|
|
198
|
+
#### Example: Listing the projects of an account
|
|
199
|
+
|
|
200
|
+
```javascript
|
|
201
|
+
theAuthAPI.projects
|
|
202
|
+
.getProjects('ACCOUNT_ID')
|
|
203
|
+
.then((projects) => console.log(projects))
|
|
204
|
+
.catch((error) => console.log(error));
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
**Using async/await**
|
|
208
|
+
|
|
209
|
+
```javascript
|
|
210
|
+
try {
|
|
211
|
+
const projects = await client.projects.getProjects('ACCOUNT_ID');
|
|
212
|
+
} catch (error) {
|
|
213
|
+
console.log(error);
|
|
214
|
+
}
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
#### Example: Listing projects and associated API-Keys
|
|
218
|
+
|
|
219
|
+
```javascript
|
|
220
|
+
async function getProjectsWithKeys(accountId: string) {
|
|
221
|
+
try {
|
|
222
|
+
const projects = await theAuthAPI.projects.getProjects(accountId);
|
|
223
|
+
const projectsKeys = projects.map(async (project) => {
|
|
224
|
+
const keys = await theAuthAPI.apiKeys.getKeys(project.id);
|
|
225
|
+
return { project, keys };
|
|
226
|
+
});
|
|
227
|
+
return await Promise.all(projectsKeys);
|
|
228
|
+
} catch (error) {
|
|
229
|
+
// handle error
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
#### Example: Creating an API-Key
|
|
235
|
+
|
|
236
|
+
```javascript
|
|
237
|
+
theAuthAPI.apiKeys
|
|
238
|
+
.createKey({
|
|
239
|
+
projectId: 'PROJECT_ID',
|
|
240
|
+
customMetaData: { metadata_val: 'value to store' },
|
|
241
|
+
customAccountId: '[any info you want]',
|
|
242
|
+
name: '[any info you want e.g. name of customer or the key]',
|
|
243
|
+
})
|
|
244
|
+
.then((key) => console.log('Key created > ', key))
|
|
245
|
+
.catch((error) => console.log("Couldn't make the key", error));
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
**Using async/await**
|
|
249
|
+
|
|
250
|
+
```javascript
|
|
251
|
+
try {
|
|
252
|
+
const key = await theAuthAPI.apiKeys.createKey({
|
|
253
|
+
projectId: 'PROJECT_ID',
|
|
254
|
+
customMetaData: { metadata_val: 'value to store' },
|
|
255
|
+
customAccountId: '[any info you want]',
|
|
256
|
+
name: '[any info you want e.g. name of customer or the key]',
|
|
257
|
+
});
|
|
258
|
+
console.log('Key created > ', key);
|
|
259
|
+
} catch (error) {
|
|
260
|
+
console.log("Couldn't make the key ", error);
|
|
261
|
+
}
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
#### Example: Rotating an API-Key
|
|
265
|
+
|
|
266
|
+
When you need to quickly and securely rotate a compromised key, while preserving the key's metadata, use the `rotateKey` method. This method while clone your key and return you with a new one.
|
|
267
|
+
|
|
268
|
+
```javascript
|
|
269
|
+
theAuthAPI.apiKeys
|
|
270
|
+
.rotateKey('API_KEY')
|
|
271
|
+
.then((key) => console.log('Rotated Key > ', key))
|
|
272
|
+
.catch((error) => console.log("Couldn't rotate the key", error));
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
**Using async/await**
|
|
276
|
+
|
|
277
|
+
```javascript
|
|
278
|
+
try {
|
|
279
|
+
const key = await theAuthAPI.apiKeys.createKey('API_KEY');
|
|
280
|
+
console.log('Rotated Key > ', key);
|
|
281
|
+
} catch (error) {
|
|
282
|
+
console.log("Couldn't rotate the key", error);
|
|
283
|
+
}
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
**NOTE** In the background, this marks the old key as inactive and issues you with a new key. Any requests to the old key will be instantly blocked.
|
|
287
|
+
|
|
288
|
+
### Handling Errors
|
|
289
|
+
|
|
290
|
+
[comment]: <> (All methods that return a promise throw 3 types of errors)
|
|
291
|
+
|
|
292
|
+
##### ApiRequestError
|
|
293
|
+
|
|
294
|
+
Thrown when there's a network or a connectivity issue, for example, if the client didn't establish any network connection with the host
|
|
295
|
+
|
|
296
|
+
```
|
|
297
|
+
ApiRequestError: getaddrinfo EAI_AGAIN api.theauthapi.com
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
##### ApiResponseError
|
|
301
|
+
|
|
302
|
+
Thrown when the server responds with an HTTP status code not in the `2xx` range. `ApiRequestError` provides two properties to distinguish the type of the error
|
|
303
|
+
|
|
304
|
+
- `statusCode` HTTP status code
|
|
305
|
+
- `message` the message the server responded with in the body
|
|
306
|
+
|
|
307
|
+
This is the most common thrown error, you should expect and handle it each time you use any of the library methods
|
|
308
|
+
|
|
309
|
+
##### Example: Getting a key throws an ApiResponseError if the key is invalid
|
|
310
|
+
|
|
311
|
+
If you try to GET an invalid key using `apiKeys.getKey("invalid-key")`, the server responds with a 404 error and an `ApiResponseError` is thrown
|
|
312
|
+
|
|
313
|
+
```
|
|
314
|
+
ApiResponseError: (404): Invalid client key
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
"404" is the `statusCode`, "Invalid client Key" is the `message`, you can access these properties using `error.statusCode` and `error.message` respectively
|
|
318
|
+
|
|
319
|
+
##### Error
|
|
320
|
+
|
|
321
|
+
Unknown error, just a normal javascript error
|
|
322
|
+
|
|
323
|
+
#### Handling Errors the Right Way
|
|
324
|
+
|
|
325
|
+
Since all the possible thrown errors are instances of classes, we can check the type of the thrown error and handle it accordingly
|
|
326
|
+
|
|
327
|
+
```javascript
|
|
328
|
+
try {
|
|
329
|
+
const key = await theAuthAPI.apiKeys.getKey('KEY');
|
|
330
|
+
} catch (error) {
|
|
331
|
+
if (error instanceof ApiResponseError) {
|
|
332
|
+
// handle response error
|
|
333
|
+
}
|
|
334
|
+
if (error instanceof ApiRequestError) {
|
|
335
|
+
// handle network error
|
|
336
|
+
}
|
|
337
|
+
// unknown error
|
|
338
|
+
throw error;
|
|
339
|
+
}
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
### Typescript
|
|
343
|
+
|
|
344
|
+
This library is written in [Typescript](https://www.typescriptlang.org/), types are provided out of the box.
|
|
345
|
+
|
|
346
|
+
Example of usage with Typescript:
|
|
347
|
+
|
|
348
|
+
```typescript
|
|
349
|
+
import TheAuthAPI from 'theauthapi';
|
|
350
|
+
import { Project } from 'theauthapi/dist/types';
|
|
351
|
+
|
|
352
|
+
const client = new TheAuthAPI('ACCESS_KEY');
|
|
353
|
+
|
|
354
|
+
async function getProjectsIds(accountId: string): Promise<string[]> {
|
|
355
|
+
const projects: Project[] = await client.projects.getProjects(accountId);
|
|
356
|
+
return projects.map((project) => project.name);
|
|
357
|
+
}
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
### 📙 Further Reading
|
|
361
|
+
|
|
362
|
+
- Create your account [https://theauthapi.com](https://theauthapi.com)
|
|
363
|
+
- View our [Knowledge Base](https://thatapicompany.notion.site/The-Auth-API-Knowledge-Base-21660cee84e640729714fad43d9ce546) help centre
|
|
364
|
+
- Read our [API docs](https://docs.theauthapi.com)
|
|
365
|
+
- Articles on best Auth practice - [https://theauthapi.com/articles](https://theauthapi.com/articles)
|
|
366
|
+
- Meet the team behind The Auth API - [That API Company](https://thatapicompany.com/)
|