Skip to content

Commit 1ebc5fb

Browse files
authored
Merge pull request #12 from unkeyed/update-to-v2-api
chore: update to v2 api
2 parents e811253 + 91dde71 commit 1ebc5fb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2420
-7776
lines changed

bun-koyeb/README.md

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
[Koyeb](https://www.koyeb.com?ref=unkey) is a developer-friendly serverless platform to deploy apps globally. Koyeb offers a fully managed environment to deploy any apps in seconds without managing any infrastructure. Koyeb supports any programming languages, frameworks, and tools to build your apps.
66

7-
This example shows how to build an API using bun, secure it with unkey, and deploy it globally on Koyeb.
8-
7+
This example shows how to build an API using Bun, secure it with Unkey, and deploy it globally on Koyeb.
98

109
## Requirements
1110

@@ -28,41 +27,48 @@ bun run dev
2827
## Test
2928

3029
```bash
31-
curl http://localhost:8000 -H "Authorization: Bearer <KEY>"
30+
curl -X POST http://localhost:8000 -H "Authorization: Bearer <KEY>"
3231
```
3332

34-
3533
## Deploy on Koyeb
3634

35+
[![Deploy to Koyeb](https://www.koyeb.com/static/images/deploy/button.svg)](https://app.koyeb.com/deploy?type=git&name=bun-unkey&service_type=web&ports=8000;http;/&env[UNKEY_ROOT_KEY]=&repository=github.com/unkeyed/unkey&branch=main&workdir=examples/bun-koyeb&builder=buildpack)
3736

38-
[![Deploy to Koyeb](https://www.koyeb.com/static/images/deploy/button.svg)](https://app.koyeb.com/deploy?type=git&name=bun-unkey&service_type=web&ports=8000;http;/&env[UNKEY_ROOT_KEY]=<root_key>&env[UNKEY_API_ID]=<api_id>&repository=github.com/unkeyed/unkey&branch=main&workdir=examples/bun-koyeb&builder=dockerfile)
39-
40-
Replace the environment variable placeholders with real values from your Unkey [dashboard](https://app.unkey.com).
37+
Replace the `UNKEY_ROOT_KEY` environment variable with your actual root key from your Unkey [dashboard](https://app.unkey.com).
4138

4239
Then hit the `Deploy` button.
43-
Koyeb will deploy your app in your selected regions and provide a unique URL to access it, or you can configure your own custom domain.
4440

41+
Koyeb will deploy your app in your selected regions and provide a unique URL to access it, or you can configure your own custom domain.
4542

4643
Now that your app is deployed, you can test it:
4744

48-
```sh-session
49-
curl -XPOST https://<YOUR_APP_NAME>-<YOUR_KOYEB_ORG>.koyeb.app -H "Authorization: Bearer <UNKEY_API_KEY>"
45+
```bash
46+
curl -X POST https://<YOUR_APP_NAME>-<YOUR_KOYEB_ORG>.koyeb.app -H "Authorization: Bearer <UNKEY_API_KEY>"
5047
```
5148

52-
It should return a `200` status code and at least the following response, depending on your key settings:
49+
It should return a `200` status code with the Unkey verification response:
50+
5351
```json
5452
{
55-
"valid": true
53+
"meta": {
54+
"requestId": "req_123"
55+
},
56+
"data": {
57+
"valid": true,
58+
"code": "VALID",
59+
"keyId": "key_123",
60+
"name": "my-api-key"
61+
}
5662
}
5763
```
5864

59-
6065
### Manual configuration:
6166

62-
1. Create a [new project](https://app.koyeb.com/apps/new) on Koyeb.
63-
2. Under the advanced section, add your `UNKEY_ROOT_KEY` and `UNKEY_API_ID` environment variables. You can find those in the [Unkey dashboard](https://app.unkey.com).
64-
3. Click on the `Deploy` button
65-
67+
1. In the [Koyeb control panel](https://app.koyeb.com), click **Create Web Service**.
68+
2. Select **GitHub** as the deployment method.
69+
3. Choose your repository and branch.
70+
4. Under **Environment variables**, add your `UNKEY_ROOT_KEY`. You can find this in the [Unkey dashboard](https://app.unkey.com).
71+
5. Click the **Deploy** button.
6672

6773
## References
6874

bun-koyeb/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
"version": "1.0.0",
44
"description": "Deploy a bun API secured by Unkey to Koyeb",
55
"scripts": {
6-
"dev": "bun run ./index.ts"
6+
"dev": "bun run ./server.ts"
77
},
88
"author": "Andreas Thomas",
99
"license": "ISC",
1010
"dependencies": {
11-
"@unkey/api": "^0.22.1"
11+
"@unkey/api": "^2.0.0"
12+
},
13+
"devDependencies": {
14+
"@types/bun": "^1.2.19"
1215
}
1316
}

bun-koyeb/server.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
import { verifyKey } from "@unkey/api";
1+
import { Unkey } from "@unkey/api";
22

33
const port = process.env.PORT || 8000;
4+
const UNKEY_ROOT_KEY = process.env.UNKEY_ROOT_KEY || "";
45

5-
console.log(`Launching Bun HTTP server on port: ${port}, url: http://0.0.0.0:${port} 🚀`);
6+
if (!UNKEY_ROOT_KEY) {
7+
console.error("UNKEY_ROOT_KEY environment variable is required");
8+
process.exit(1);
9+
}
10+
11+
const unkey = new Unkey({ rootKey: UNKEY_ROOT_KEY });
12+
13+
console.log(
14+
`Launching Bun HTTP server on port: ${port}, url: http://0.0.0.0:${port} 🚀`,
15+
);
616

717
Bun.serve({
818
async fetch(req) {
@@ -11,19 +21,18 @@ Bun.serve({
1121
return new Response("Unauthorized", { status: 401 });
1222
}
1323

14-
const { result, error } = await verifyKey(key);
15-
if (error) {
16-
// This may happen on network errors
17-
// We already retry the request 5 times, but if it still fails, we return an error
18-
console.error(error);
19-
return Response.json("Internal Server Error", { status: 500 });
20-
}
24+
try {
25+
const response = await unkey.keys.verifyKey({ key });
2126

22-
if (!result.valid) {
23-
return new Response("Unauthorized", { status: 401 });
24-
}
27+
if (!response.data.valid) {
28+
return new Response("Unauthorized", { status: 401 });
29+
}
2530

26-
return Response.json(result);
31+
return Response.json(response);
32+
} catch (error) {
33+
console.error("Key verification error:", error);
34+
return new Response("Internal Server Error", { status: 500 });
35+
}
2736
},
28-
port: 8000,
37+
port,
2938
});

bun-koyeb/tsconfig.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"compilerOptions": {
3+
// Environment setup & latest features
4+
"lib": ["ESNext"],
5+
"target": "ESNext",
6+
"module": "Preserve",
7+
"moduleDetection": "force",
8+
"jsx": "react-jsx",
9+
"allowJs": true,
10+
11+
// Bundler mode
12+
"moduleResolution": "bundler",
13+
"allowImportingTsExtensions": true,
14+
"verbatimModuleSyntax": true,
15+
"noEmit": true,
16+
17+
// Best practices
18+
"strict": true,
19+
"skipLibCheck": true,
20+
"noFallthroughCasesInSwitch": true,
21+
"noUncheckedIndexedAccess": true,
22+
"noImplicitOverride": true,
23+
24+
// Some stricter flags (disabled by default)
25+
"noUnusedLocals": false,
26+
"noUnusedParameters": false,
27+
"noPropertyAccessFromIndexSignature": false
28+
}
29+
}

bun-server/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
"version": "1.0.0",
44
"description": "Bun server with unkey.dev",
55
"scripts": {
6-
"dev": "bun run ./index.ts"
6+
"dev": "bun run ./server.ts"
77
},
88
"author": "Andreas Thomas",
99
"license": "ISC",
1010
"dependencies": {
11-
"@unkey/api": "^0.22.1"
11+
"@unkey/api": "^2.0.0"
12+
},
13+
"devDependencies": {
14+
"@types/bun": "^1.2.19"
1215
}
1316
}

bun-server/server.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
import { verifyKey } from "@unkey/api";
1+
import { Unkey } from "@unkey/api";
22

33
const port = process.env.PORT || 8000;
4+
const UNKEY_ROOT_KEY = process.env.UNKEY_ROOT_KEY || "";
45

5-
console.log(`Launching Bun HTTP server on port: ${port}, url: http://0.0.0.0:${port} 🚀`);
6+
if (!UNKEY_ROOT_KEY) {
7+
console.error("UNKEY_ROOT_KEY environment variable is required");
8+
process.exit(1);
9+
}
10+
11+
const unkey = new Unkey({ rootKey: UNKEY_ROOT_KEY });
12+
13+
console.log(
14+
`Launching Bun HTTP server on port: ${port}, url: http://0.0.0.0:${port} 🚀`,
15+
);
616

717
Bun.serve({
818
async fetch(req) {
@@ -11,19 +21,18 @@ Bun.serve({
1121
return new Response("Unauthorized", { status: 401 });
1222
}
1323

14-
const { result, error } = await verifyKey(key);
15-
if (error) {
16-
// This may happen on network errors
17-
// We already retry the request 5 times, but if it still fails, we return an error
18-
console.error(error);
19-
return Response.json("Internal Server Error", { status: 500 });
20-
}
24+
try {
25+
const response = await unkey.keys.verifyKey({ key });
2126

22-
if (!result.valid) {
23-
return new Response("Unauthorized", { status: 401 });
24-
}
27+
if (!response.data.valid) {
28+
return new Response("Unauthorized", { status: 401 });
29+
}
2530

26-
return Response.json(result);
31+
return Response.json(response);
32+
} catch (error) {
33+
console.error("Key verification error:", error);
34+
return new Response("Internal Server Error", { status: 500 });
35+
}
2736
},
28-
port: 8000,
37+
port,
2938
});

bun-server/tsconfig.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"compilerOptions": {
3+
// Environment setup & latest features
4+
"lib": ["ESNext"],
5+
"target": "ESNext",
6+
"module": "Preserve",
7+
"moduleDetection": "force",
8+
"jsx": "react-jsx",
9+
"allowJs": true,
10+
11+
// Bundler mode
12+
"moduleResolution": "bundler",
13+
"allowImportingTsExtensions": true,
14+
"verbatimModuleSyntax": true,
15+
"noEmit": true,
16+
17+
// Best practices
18+
"strict": true,
19+
"skipLibCheck": true,
20+
"noFallthroughCasesInSwitch": true,
21+
"noUncheckedIndexedAccess": true,
22+
"noImplicitOverride": true,
23+
24+
// Some stricter flags (disabled by default)
25+
"noUnusedLocals": false,
26+
"noUnusedParameters": false,
27+
"noPropertyAccessFromIndexSignature": false
28+
}
29+
}

cloudflare/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
"devDependencies": {
1010
"@cloudflare/workers-types": "^4.20231016.0",
1111
"typescript": "^5.0.4",
12-
"wrangler": "^3.14.0"
12+
"wrangler": "^3.114.12"
1313
},
1414
"dependencies": {
15-
"@unkey/api": "^0.22.1",
15+
"@unkey/api": "^2.0.0",
1616
"deploy": "^1.0.3",
1717
"pnpm": "^8.9.2",
1818
"run": "^1.4.0"

cloudflare/src/worker.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,41 @@
1-
import { verifyKey } from "@unkey/api";
1+
import { Unkey } from "@unkey/api";
22

33
export default {
4-
async fetch(request: Request): Promise<Response> {
4+
async fetch(request: Request, env: Env): Promise<Response> {
5+
const UNKEY_ROOT_KEY = env.UNKEY_ROOT_KEY;
6+
7+
if (!UNKEY_ROOT_KEY) {
8+
return new Response("Server configuration error", { status: 500 });
9+
}
10+
11+
const unkey = new Unkey({ rootKey: UNKEY_ROOT_KEY });
12+
513
// Grab the key from the authorization header
614
const authHeader = request.headers.get("Authorization");
715
if (!authHeader) {
816
return new Response("Unauthorized (No key)", { status: 401 });
917
}
18+
1019
const key = authHeader.replace("Bearer ", "");
1120

12-
const { result, error } = await verifyKey(key);
13-
if (error) {
14-
console.error(error.message);
21+
try {
22+
const response = await unkey.keys.verifyKey({ key });
23+
24+
if (!response.data.valid) {
25+
return new Response("Unauthorized", { status: 401 });
26+
}
27+
28+
// proceed to handle the request
29+
// since this is a demo, we just return the result
30+
return Response.json(response.data);
31+
} catch (error) {
32+
console.error("Key verification error:", error);
1533
return new Response("Internal Server Error", { status: 500 });
1634
}
17-
if (!result.valid) {
18-
return new Response("Unauthorized", { status: 401 });
19-
}
20-
21-
// proceed to handle the request
22-
// since this is a demo, we just return the result
23-
return Response.json(result);
2435
},
2536
};
37+
38+
// Type definition for the environment
39+
interface Env {
40+
UNKEY_ROOT_KEY: string;
41+
}

cloudflare/wrangler.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
name = "cloudflare2"
22
main = "src/worker.ts"
33
compatibility_date = "2023-10-17"
4+
5+
6+
[vars]
7+
UNKEY_ROOT_KEY = "<YOUR_UNKEY_ROOT_KEY_HERE>"

0 commit comments

Comments
 (0)