|
| 1 | +--- |
| 2 | +title: Grok |
| 3 | +pcx_content_type: get-started |
| 4 | +sidebar: |
| 5 | + badge: |
| 6 | + text: Beta |
| 7 | +--- |
| 8 | + |
| 9 | +[Grok](https://docs.x.ai/docs#getting-started) is s a general purpose model that can be used for a variety of tasks, including generating and understanding text, code, and function calling. |
| 10 | + |
| 11 | +## Endpoint |
| 12 | + |
| 13 | +`https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok` |
| 14 | + |
| 15 | +## URL structure |
| 16 | + |
| 17 | +When making requests to [Grok](https://docs.x.ai/docs#getting-started), replace `https://api.x.ai/v1` in the URL you are currently using with `https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok`. |
| 18 | + |
| 19 | +## Examples |
| 20 | + |
| 21 | +### cURL |
| 22 | + |
| 23 | +```bash title="Request" |
| 24 | +curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok/v1/chat/completions \ |
| 25 | + --header 'content-type: application/json' \ |
| 26 | + --header 'Authorization: Bearer {grok_api_token}' \ |
| 27 | + --data '{ |
| 28 | + "model": "grok-beta", |
| 29 | + "messages": [ |
| 30 | + { |
| 31 | + "role": "user", |
| 32 | + "content": "What is Cloudflare?" |
| 33 | + } |
| 34 | + ] |
| 35 | +}' |
| 36 | +``` |
| 37 | + |
| 38 | +If you are using the OpenAI SDK with JavaScript, you can set your endpoint like this: |
| 39 | + |
| 40 | +```js title="JavaScript" |
| 41 | +import OpenAI from "openai"; |
| 42 | + |
| 43 | +const openai = new OpenAI({ |
| 44 | + apiKey: "<api key>", |
| 45 | + baseURL: |
| 46 | + "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok", |
| 47 | +}); |
| 48 | + |
| 49 | +const completion = await openai.chat.completions.create({ |
| 50 | + model: "grok-beta", |
| 51 | + messages: [ |
| 52 | + { |
| 53 | + role: "system", |
| 54 | + content: |
| 55 | + "You are Grok, a chatbot inspired by the Hitchhiker's Guide to the Galaxy.", |
| 56 | + }, |
| 57 | + { |
| 58 | + role: "user", |
| 59 | + content: "What is the meaning of life, the universe, and everything?", |
| 60 | + }, |
| 61 | + ], |
| 62 | +}); |
| 63 | + |
| 64 | +console.log(completion.choices[0].message); |
| 65 | +``` |
| 66 | + |
| 67 | +If you are using the OpenAI SDK with Python, you can set your endpoint like this: |
| 68 | + |
| 69 | +```python title="Python" |
| 70 | +import os |
| 71 | +from openai import OpenAI |
| 72 | + |
| 73 | +XAI_API_KEY = os.getenv("XAI_API_KEY") |
| 74 | +client = OpenAI( |
| 75 | + api_key=XAI_API_KEY, |
| 76 | + base_url="https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok", |
| 77 | +) |
| 78 | + |
| 79 | +completion = client.chat.completions.create( |
| 80 | + model="grok-beta", |
| 81 | + messages=[ |
| 82 | + {"role": "system", "content": "You are Grok, a chatbot inspired by the Hitchhiker's Guide to the Galaxy."}, |
| 83 | + {"role": "user", "content": "What is the meaning of life, the universe, and everything?"}, |
| 84 | + ], |
| 85 | +) |
| 86 | + |
| 87 | +print(completion.choices[0].message) |
| 88 | +``` |
| 89 | + |
| 90 | +If you are using the Anthropic SDK with JavaScript, you can set your endpoint like this: |
| 91 | + |
| 92 | +```js title="JavaScript" |
| 93 | +import Anthropic from "@anthropic-ai/sdk"; |
| 94 | + |
| 95 | +const anthropic = new Anthropic({ |
| 96 | + apiKey: "<api key>", |
| 97 | + baseURL: |
| 98 | + "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok", |
| 99 | +}); |
| 100 | + |
| 101 | +const msg = await anthropic.messages.create({ |
| 102 | + model: "grok-beta", |
| 103 | + max_tokens: 128, |
| 104 | + system: |
| 105 | + "You are Grok, a chatbot inspired by the Hitchhiker's Guide to the Galaxy.", |
| 106 | + messages: [ |
| 107 | + { |
| 108 | + role: "user", |
| 109 | + content: "What is the meaning of life, the universe, and everything?", |
| 110 | + }, |
| 111 | + ], |
| 112 | +}); |
| 113 | + |
| 114 | +console.log(msg); |
| 115 | +``` |
| 116 | + |
| 117 | +If you are using the Anthropic SDK with Python, you can set your endpoint like this: |
| 118 | + |
| 119 | +```python title="Python" |
| 120 | +import os |
| 121 | +from anthropic import Anthropic |
| 122 | + |
| 123 | +XAI_API_KEY = os.getenv("XAI_API_KEY") |
| 124 | +client = Anthropic( |
| 125 | + api_key=XAI_API_KEY, |
| 126 | + base_url="https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok", |
| 127 | +) |
| 128 | + |
| 129 | +message = client.messages.create( |
| 130 | + model="grok-beta", |
| 131 | + max_tokens=128, |
| 132 | + system="You are Grok, a chatbot inspired by the Hitchhiker's Guide to the Galaxy.", |
| 133 | + messages=[ |
| 134 | + { |
| 135 | + "role": "user", |
| 136 | + "content": "What is the meaning of life, the universe, and everything?", |
| 137 | + }, |
| 138 | + ], |
| 139 | +) |
| 140 | + |
| 141 | +print(message.content) |
| 142 | +``` |
0 commit comments