|
| 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 -X POST 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 | +### JavaScript (OpenAI SDK) |
| 39 | + |
| 40 | +```js title="JavaScript" |
| 41 | +import OpenAI from "openai"; |
| 42 | + |
| 43 | +const openai = new OpenAI({ |
| 44 | + apiKey: "<api key>", |
| 45 | + baseURL: "https://api.x.ai/v1", |
| 46 | +}); |
| 47 | + |
| 48 | +const completion = await openai.chat.completions.create({ |
| 49 | + model: "grok-beta", |
| 50 | + messages: [ |
| 51 | + { |
| 52 | + role: "system", |
| 53 | + content: |
| 54 | + "You are Grok, a chatbot inspired by the Hitchhiker's Guide to the Galaxy.", |
| 55 | + }, |
| 56 | + { |
| 57 | + role: "user", |
| 58 | + content: "What is the meaning of life, the universe, and everything?", |
| 59 | + }, |
| 60 | + ], |
| 61 | +}); |
| 62 | + |
| 63 | +console.log(completion.choices[0].message); |
| 64 | +``` |
| 65 | + |
| 66 | +### Python (OpenAI SDK) |
| 67 | + |
| 68 | +```python title="Python" |
| 69 | +import os |
| 70 | +from openai import OpenAI |
| 71 | + |
| 72 | +XAI_API_KEY = os.getenv("XAI_API_KEY") |
| 73 | +client = OpenAI( |
| 74 | + api_key=XAI_API_KEY, |
| 75 | + base_url="https://api.x.ai/v1", |
| 76 | +) |
| 77 | + |
| 78 | +completion = client.chat.completions.create( |
| 79 | + model="grok-beta", |
| 80 | + messages=[ |
| 81 | + {"role": "system", "content": "You are Grok, a chatbot inspired by the Hitchhiker's Guide to the Galaxy."}, |
| 82 | + {"role": "user", "content": "What is the meaning of life, the universe, and everything?"}, |
| 83 | + ], |
| 84 | +) |
| 85 | + |
| 86 | +print(completion.choices[0].message) |
| 87 | +``` |
| 88 | + |
| 89 | +### JavaScript (Anthropic SDK) |
| 90 | + |
| 91 | +```js title="JavaScript" |
| 92 | +import Anthropic from "@anthropic-ai/sdk"; |
| 93 | + |
| 94 | +const anthropic = new Anthropic({ |
| 95 | + apiKey: "<api key>", |
| 96 | + baseURL: "https://api.x.ai/", |
| 97 | +}); |
| 98 | + |
| 99 | +const msg = await anthropic.messages.create({ |
| 100 | + model: "grok-beta", |
| 101 | + max_tokens: 128, |
| 102 | + system: |
| 103 | + "You are Grok, a chatbot inspired by the Hitchhiker's Guide to the Galaxy.", |
| 104 | + messages: [ |
| 105 | + { |
| 106 | + role: "user", |
| 107 | + content: "What is the meaning of life, the universe, and everything?", |
| 108 | + }, |
| 109 | + ], |
| 110 | +}); |
| 111 | + |
| 112 | +console.log(msg); |
| 113 | +``` |
| 114 | + |
| 115 | +### Python (Anthropic SDK) |
| 116 | + |
| 117 | +```python title="Python" |
| 118 | +import os |
| 119 | +from anthropic import Anthropic |
| 120 | + |
| 121 | +XAI_API_KEY = os.getenv("XAI_API_KEY") |
| 122 | +client = Anthropic( |
| 123 | + api_key=XAI_API_KEY, |
| 124 | + base_url="https://api.x.ai", |
| 125 | +) |
| 126 | + |
| 127 | +message = client.messages.create( |
| 128 | + model="grok-beta", |
| 129 | + max_tokens=128, |
| 130 | + system="You are Grok, a chatbot inspired by the Hitchhiker's Guide to the Galaxy.", |
| 131 | + messages=[ |
| 132 | + { |
| 133 | + "role": "user", |
| 134 | + "content": "What is the meaning of life, the universe, and everything?", |
| 135 | + }, |
| 136 | + ], |
| 137 | +) |
| 138 | + |
| 139 | +print(message.content) |
| 140 | +``` |
0 commit comments