theauthapi 1.0.12 → 1.0.13

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