You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This guide provides an overview of how to use the latest Cloudflare Workers AI Gateway binding methods. You will learn how to set up an AI Gateway binding, access new methods, and integrate them into your Workers.
10
10
11
-
## Prerequisites
12
-
13
-
- Install and use the `@cloudflare/workers-types` library, version `4.20250124.3` or above.
14
-
15
11
## 1. Add an AI Binding to your Worker
16
12
17
13
To connect your Worker to Workers AI, add the following to your [Wrangler configuration file](/workers/wrangler/configuration/):
@@ -34,13 +30,17 @@ This configuration sets up the AI binding accessible in your Worker code as `env
34
30
To perform an inference task using Workers AI and an AI Gateway, you can use the following code:
Additionally, you can access the latest request log ID with:
@@ -64,12 +64,12 @@ Once you have the gateway instance, you can use the following methods:
64
64
The `patchLog` method allows you to send feedback, score, and metadata for a specific log ID. All object properties are optional, so you can include any combination of the parameters:
65
65
66
66
```typescript
67
-
gateway.patchLog('my-log-id', {
68
-
feedback: 1,
69
-
score: 100,
70
-
metadata: {
71
-
user: "123"
72
-
}
67
+
gateway.patchLog("my-log-id", {
68
+
feedback: 1,
69
+
score: 100,
70
+
metadata: {
71
+
user: "123",
72
+
},
73
73
});
74
74
```
75
75
@@ -78,7 +78,7 @@ gateway.patchLog('my-log-id', {
78
78
79
79
### 3.2. `getLog`: Read Log Details
80
80
81
-
The `getLog` method retrieves details of a specific log ID. It returns an object of type `Promise<AiGatewayLog>`. You can import the `AiGatewayLog` type from the `@cloudflare/workers-types` library.
81
+
The `getLog` method retrieves details of a specific log ID. It returns an object of type `Promise<AiGatewayLog>`. If this type is missing, ensure you have run [`wrangler types`](/workers/languages/typescript/#generate-types)
@@ -145,14 +148,14 @@ Refer to the [Universal endpoint documentation](/ai-gateway/providers/universal/
145
148
146
149
```typescript
147
150
const resp =awaitgateway.run({
148
-
provider: "workers-ai",
149
-
endpoint: "@cf/meta/llama-3.1-8b-instruct",
150
-
headers: {
151
-
authorization: "Bearer my-api-token"
152
-
},
153
-
query: {
154
-
prompt: "tell me a joke"
155
-
}
151
+
provider: "workers-ai",
152
+
endpoint: "@cf/meta/llama-3.1-8b-instruct",
153
+
headers: {
154
+
authorization: "Bearer my-api-token",
155
+
},
156
+
query: {
157
+
prompt: "tell me a joke",
158
+
},
156
159
});
157
160
```
158
161
@@ -168,4 +171,4 @@ With these AI Gateway binding methods, you can now:
168
171
- Get gateway URLs for direct API access with `getUrl`, making it easy to integrate with popular AI SDKs.
169
172
- Execute universal requests to any AI Gateway provider with `run`.
170
173
171
-
These methods offer greater flexibility and control over your AI integrations, empowering you to build more sophisticated applications on the Cloudflare Workers platform.
174
+
These methods offer greater flexibility and control over your AI integrations, empowering you to build more sophisticated applications on the Cloudflare Workers platform.
Copy file name to clipboardExpand all lines: src/content/docs/d1/worker-api/index.mdx
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,7 @@ Refer to the relevant sections for the API documentation.
18
18
19
19
## TypeScript support
20
20
21
-
D1 Worker Bindings API is fully-typed via the [`@cloudflare/workers-types`](/workers/languages/typescript/#typescript) package, and also supports [generic types](https://www.typescriptlang.org/docs/handbook/2/generics.html#generic-types) as part of its TypeScript API. A generic type allows you to provide an optional `type parameter` so that a function understands the type of the data it is handling.
21
+
D1 Worker Bindings API is fully-typed via the types generated by running [`wrangler types`](/workers/languages/typescript/#typescript) package, and also supports [generic types](https://www.typescriptlang.org/docs/handbook/2/generics.html#generic-types) as part of its TypeScript API. A generic type allows you to provide an optional `type parameter` so that a function understands the type of the data it is handling.
22
22
23
23
When using the query statement methods [`D1PreparedStatement::run`](/d1/worker-api/prepared-statements/#run), [`D1PreparedStatement::raw`](/d1/worker-api/prepared-statements/#raw) and [`D1PreparedStatement::first`](/d1/worker-api/prepared-statements/#first), you can provide a type representing each database row. D1's API will [return the result object](/d1/worker-api/return-object/#d1result) with the correct type.
Copy file name to clipboardExpand all lines: src/content/docs/pages/framework-guides/deploy-a-nuxt-site.mdx
+1-7Lines changed: 1 addition & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -120,17 +120,11 @@ In order to access bindings in a deployed application, you will need to [configu
120
120
121
121
### Add bindings to TypeScript projects
122
122
123
-
To get proper type support, you need to create a new `env.d.ts` file in the root of your project and declare a [binding](/pages/functions/bindings/).
123
+
To get proper type support, you need to create a new `env.d.ts` file in the root of your project and declare a [binding](/pages/functions/bindings/). Make sure you have generated Cloudflare runtime types by running [`wrangler types`](/pages/functions/typescript/).
124
124
125
125
The following is an example of adding a `KVNamespace` binding:
Copy file name to clipboardExpand all lines: src/content/docs/pages/framework-guides/deploy-an-astro-site.mdx
+1-3Lines changed: 1 addition & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -113,12 +113,11 @@ Use bindings in Astro components and API routes by using `context.locals` from [
113
113
114
114
Refer to the following example of how to access a KV namespace with TypeScript.
115
115
116
-
First, you need to define Cloudflare runtime and KV type by updating the `env.d.ts`:
116
+
First, you need to define Cloudflare runtime and KV type by updating the `env.d.ts`. Make sure you have generated Cloudflare runtime types by running [`wrangler types`](/pages/functions/typescript/).
Copy file name to clipboardExpand all lines: src/content/docs/pages/framework-guides/nextjs/ssr/bindings.mdx
+1-26Lines changed: 1 addition & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,32 +30,7 @@ Add bindings to your Pages project by adding them to your [Wrangler configuratio
30
30
31
31
## TypeScript type declarations for bindings
32
32
33
-
To ensure that the `env` object from `getRequestContext().env` above has accurate TypeScript types, install [`@cloudflare/workers-types`](https://www.npmjs.com/package/@cloudflare/workers-types) and create a [TypeScript declaration file](https://www.typescriptlang.org/docs/handbook/2/type-declarations.html).
34
-
35
-
Install Workers Types:
36
-
37
-
```sh
38
-
npm install --save-dev @cloudflare/workers-types
39
-
```
40
-
41
-
Add Workers Types to your `tsconfig.json` file, replacing the date below with your project's [compatibility date](/workers/configuration/compatibility-dates/):
42
-
43
-
```diff title="tsconfig.json"
44
-
"types": [
45
-
+ "@cloudflare/workers-types/2024-07-29"
46
-
]
47
-
```
48
-
49
-
Create an `env.d.ts` file in the root directory of your Next.js app, and explicitly declare the type of each binding:
50
-
51
-
```ts title="env.d.ts"
52
-
interfaceCloudflareEnv {
53
-
MY_KV_1:KVNamespace;
54
-
MY_KV_2:KVNamespace;
55
-
MY_R2:R2Bucket;
56
-
MY_DO:DurableObjectNamespace;
57
-
}
58
-
```
33
+
To ensure that the `env` object from `getRequestContext().env` above has accurate TypeScript types, make sure you have generated types by running [`wrangler types`](/workers/languages/typescript/#generate-types) and followed the setup steps.
Copy file name to clipboardExpand all lines: src/content/docs/vectorize/reference/client-api.mdx
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -220,4 +220,4 @@ Refer to the [bindings documentation](/workers/wrangler/configuration/#vectorize
220
220
221
221
New Workers projects created via `npm create cloudflare@latest` automatically include the relevant TypeScript types for Vectorize.
222
222
223
-
Older projects, or non-Workers projects looking to use Vectorize's [REST API](https://developers.cloudflare.com/api/resources/vectorize/subresources/indexes/methods/list/) in a TypeScript project, should ensure `@cloudflare/workers-types` version `4.20230922.0` or later is installed.
223
+
If you have an older project, or a non-Workers projects looking to use Vectorize's [REST API](https://developers.cloudflare.com/api/resources/vectorize/subresources/indexes/methods/list/) in a TypeScript project, you should ensure you have a compatibility date later than `2023-09-22` and that you have generated types by running [`wrangler types`](/workers/languages/typescript/#generate-types).
Invalid or incorrectly-named keys in the `cf` object will be silently ignored. Consider using TypeScript and [`@cloudflare/workers-types`](https://www.npmjs.com/package/@cloudflare/workers-types) to ensure proper use of the `cf` object.
102
+
Invalid or incorrectly-named keys in the `cf` object will be silently ignored. Consider using TypeScript and generating types by running [`wrangler types`](/workers/languages/typescript/#generate-types) to ensure proper use of the `cf` object.
Copy file name to clipboardExpand all lines: src/content/docs/workers/runtime-apis/rpc/typescript.mdx
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,7 @@ description: How TypeScript types for your Worker or Durable Object's RPC
11
11
12
12
---
13
13
14
-
The [`@cloudflare/workers-types`](https://www.npmjs.com/package/@cloudflare/workers-types)package provides the `Service` and `DurableObjectNamespace` types, each of which accepts a single type parameter for the server-side [`WorkerEntrypoint`](/workers/runtime-apis/bindings/service-bindings/rpc) or [`DurableObject`](/durable-objects/best-practices/create-durable-object-stubs-and-send-requests/#call-rpc-methods) types.
14
+
The types generated by running [`wrangler types`](/workers/languages/typescript/#generate-types)provide the `Service` and `DurableObjectNamespace` types, each of which accepts a single type parameter for the server-side [`WorkerEntrypoint`](/workers/runtime-apis/bindings/service-bindings/rpc) or [`DurableObject`](/durable-objects/best-practices/create-durable-object-stubs-and-send-requests/#call-rpc-methods) types.
15
15
16
16
Using higher-order types, we automatically generate client-side stub types (e.g., forcing all methods to be async).
0 commit comments