Skip to content

Commit 84a2092

Browse files
authored
Add ai gateway get url binding method docs (#21293)
1 parent 0dbcd77 commit 84a2092

File tree

1 file changed

+55
-5
lines changed

1 file changed

+55
-5
lines changed

src/content/docs/ai-gateway/integrations/worker-binding-methods.mdx

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: AI Gateway Binding Methods
33
pcx_content_type: tutorial
4-
updated: 2025-01-28
4+
updated: 2025-04-01
55
---
66

77
import { Render, PackageManagers } from "~/components";
@@ -87,7 +87,57 @@ const log = await gateway.getLog("my-log-id");
8787
- **Returns**: `Promise<AiGatewayLog>`
8888
- **Example Use Case**: Retrieve log information for debugging or analytics.
8989

90-
### 3.3. `run`: Universal Requests
90+
### 3.3. `getUrl`: Get Gateway URLs
91+
92+
The `getUrl` method allows you to retrieve the base URL for your AI Gateway, optionally specifying a provider to get the provider-specific endpoint.
93+
94+
```typescript
95+
// Get the base gateway URL
96+
const baseUrl = await gateway.getUrl();
97+
// Output: https://gateway.ai.cloudflare.com/v1/my-account-id/my-gateway/
98+
99+
// Get a provider-specific URL
100+
const openaiUrl = await gateway.getUrl('openai');
101+
// Output: https://gateway.ai.cloudflare.com/v1/my-account-id/my-gateway/openai
102+
```
103+
104+
- **Parameters**: Optional `provider` (string or `AIGatewayProviders` enum)
105+
- **Returns**: `Promise<string>`
106+
- **Example Use Case**: Dynamically construct URLs for direct API calls or debugging configurations.
107+
108+
#### SDK Integration Examples
109+
110+
The `getUrl` method is particularly useful for integrating with popular AI SDKs:
111+
112+
**OpenAI SDK:**
113+
```typescript
114+
import OpenAI from "openai";
115+
116+
const openai = new OpenAI({
117+
apiKey: "my api key", // defaults to process.env["OPENAI_API_KEY"]
118+
baseURL: await env.AI.gateway('my-gateway').getUrl('openai'),
119+
});
120+
```
121+
122+
**Vercel AI SDK with OpenAI:**
123+
```typescript
124+
import { createOpenAI } from "@ai-sdk/openai";
125+
126+
const openai = createOpenAI({
127+
baseURL: await env.AI.gateway('my-gateway').getUrl('openai'),
128+
});
129+
```
130+
131+
**Vercel AI SDK with Anthropic:**
132+
```typescript
133+
import { createAnthropic } from "@ai-sdk/anthropic";
134+
135+
const anthropic = createAnthropic({
136+
baseURL: await env.AI.gateway('my-gateway').getUrl('anthropic'),
137+
});
138+
```
139+
140+
### 3.4. `run`: Universal Requests
91141

92142
The `run` method allows you to execute universal requests. Users can pass either a single universal request object or an array of them. This method supports all AI Gateway providers.
93143

@@ -111,11 +161,11 @@ const resp = await gateway.run({
111161

112162
## Conclusion
113163

114-
With the new AI Gateway binding methods, you can now:
164+
With these AI Gateway binding methods, you can now:
115165

116166
- Send feedback and update metadata with `patchLog`.
117167
- Retrieve detailed log information using `getLog`.
168+
- Get gateway URLs for direct API access with `getUrl`, making it easy to integrate with popular AI SDKs.
118169
- Execute universal requests to any AI Gateway provider with `run`.
119170

120-
These methods offer greater flexibility and control over your AI integrations, empowering you to build more sophisticated applications on the Cloudflare Workers platform.
121-
171+
These methods offer greater flexibility and control over your AI integrations, empowering you to build more sophisticated applications on the Cloudflare Workers platform.

0 commit comments

Comments
 (0)