Skip to content

Commit 08ae100

Browse files
committed
added client lib
1 parent 08239c8 commit 08ae100

2 files changed

Lines changed: 117 additions & 0 deletions

File tree

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,55 @@ Designed for simplicity, privacy, and host control, AuthCell runs as a single co
2424

2525
---
2626

27+
## Client Libraries
28+
29+
AuthCell includes lightweight client libraries to simplify integration into your existing applications.
30+
Clients automatically deduce the server URL using the `AUTHCELL_URL` environment variable or fallback to `http://localhost:8080/v1`.
31+
32+
---
33+
34+
### Node.js / TypeScript Client
35+
36+
**File:** `client/nodejs/index.ts`
37+
**Dependencies:** None (uses native `fetch`, Node.js v18+ required)
38+
39+
**Example Use**
40+
41+
```typescript
42+
import { AuthCellClient } from "./client/nodejs/index";
43+
44+
const client = new AuthCellClient(); // will use AUTHCELL_URL or localhost
45+
46+
async function main() {
47+
const created = await client.createKey({ key_prefix: "demo_" });
48+
console.log("Created key:", created.api_key);
49+
50+
const verified = await client.verifyKey(created.api_key);
51+
console.log("Verification:", verified.valid);
52+
}
53+
54+
main();
55+
```
56+
57+
---
58+
59+
### Planned Client SDKs
60+
61+
| Language | Status | Description |
62+
| ---------------------- | ----------------------------- | ------------------------------------------------- |
63+
| **Node.js/TypeScript** | ✅ In repo (`client/nodejs/`) | Native fetch API, zero dependencies |
64+
| **Python** | Planned | HTTPx/Requests wrapper with auto server detection |
65+
66+
---
67+
68+
### Example ENV Configuration for Clients
69+
70+
```bash
71+
export AUTHCELL_URL="https://authcell.mydomain.com/v1"
72+
```
73+
74+
The client will automatically resolve this; no manual configuration required.
75+
2776
## API Endpoints
2877

2978
API Documentation: [View in Postman Documenter](https://documenter.getpostman.com/view/28328727/2sB3QQJnT4#be11f7c6-14f0-4a50-9d05-a726046306fa)

client/nodejs/index.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
export interface APIKeyCreateResponse {
2+
api_key: string;
3+
id: string;
4+
key_id: string;
5+
}
6+
7+
export interface APIKeyVerifyResponse {
8+
valid: boolean;
9+
}
10+
11+
export class AuthCellClient {
12+
private baseUrl: string;
13+
14+
constructor(baseUrl?: string) {
15+
this.baseUrl =
16+
baseUrl || process.env.AUTHCELL_URL || "http://localhost:8080/v1";
17+
}
18+
19+
private async __request<T>(
20+
path: string,
21+
method: string,
22+
body?: unknown,
23+
): Promise<T> {
24+
const headers = { "Content-Type": "application/json" };
25+
const url = `${this.baseUrl}${path}`;
26+
const res = await fetch(url, {
27+
method,
28+
headers,
29+
body: body ? JSON.stringify(body) : undefined,
30+
});
31+
if (!res.ok) {
32+
const err = await res.text();
33+
throw new Error(`HTTP ${res.status}: ${err}`);
34+
}
35+
const data = await res.json();
36+
return data.data as T;
37+
}
38+
39+
async createKey(opts: {
40+
key_prefix: string;
41+
expires_at?: string;
42+
rate_limit?: number;
43+
}): Promise<APIKeyCreateResponse> {
44+
return this.__request<APIKeyCreateResponse>("/keys", "POST", opts);
45+
}
46+
47+
async verifyKey(api_key: string): Promise<APIKeyVerifyResponse> {
48+
return this.__request<APIKeyVerifyResponse>("/keys/verify", "POST", {
49+
api_key,
50+
});
51+
}
52+
53+
async getKey(id: string) {
54+
return this.__request(`/keys/${id}`, "GET");
55+
}
56+
57+
async revokeKey(id: string) {
58+
return this.__request(`/keys/${id}`, "DELETE");
59+
}
60+
61+
async listUsage(id: string) {
62+
return this.__request(`/usage/${id}`, "GET");
63+
}
64+
65+
async listAuditLog() {
66+
return this.__request("/audit", "GET");
67+
}
68+
}

0 commit comments

Comments
 (0)