> ## Documentation Index
> Fetch the complete documentation index at: https://docs.obiguard.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# xAI

> Obiguard supports xAI's chat completions, completions, and embeddings APIs.

<Note>
  Provider Slug. `x-ai`
</Note>

## Integrate

Just paste your xAI API Key to [Obiguard](/virtual-keys) to create your Virtual Key.

## Sample Request

Obiguard is a drop-in replacement for xAI. You can make request using the official Obiguard SDK.

<Note>
  Popular libraries & agent frameworks like LangChain, CrewAI, AutoGen, etc. are [also supported](#popular-libraries).
</Note>

<Tabs>
  <Tab title="Python SDK">
    ```py Python theme={null}
    from obiguard import Obiguard

    client = Obiguard(
      obiguard_api_key="vk-obg***",  # Your Obiguard virtual key
    )

    response = client.chat.completions.create(
      model="grok-beta",
      messages=[{"role": "user", "content": "Hello!"}]
    )

    print(response.choices[0].message)
    ```
  </Tab>

  <Tab title="OpenAI SDK">
    ```py OpenAI Python SDK theme={null}
    from openai import OpenAI
    from obiguard import OBIGUARD_GATEWAY_URL, createHeaders

    client = OpenAI(
      api_key='OPENAI_API_KEY',
      base_url=OBIGUARD_GATEWAY_URL,
      default_headers=createHeaders(
        obiguard_api_key="vk-obg***",  # Your Obiguard virtual key
      )
    )

    completion = client.chat.completions.create(
      model="grok-beta",
      messages=[
        {"role": "user", "content": "Hello!"}
      ]
    )

    print(completion.choices[0].message)
    ```
  </Tab>

  <Tab title="cURL">
    ```sh cURL theme={null}
    curl https://gateway.obiguard.ai/v1/chat/completions \
      -H "Content-Type: application/json" \
      -H "x-obiguard-api-key: $OBIGUARD_API_KEY" \
      -d '{
        "model": "grok-beta",
        "messages": [
          {"role": "user", "content": "Hello!"}
        ]
      }'
    ```
  </Tab>
</Tabs>

## Integration Overview

### xAI Endpoints & Capabilities

Obiguard works with *all* of xAI's endpoints and supports all xAI capabilities like function calling and image understanding. Find examples for each below:

<AccordionGroup>
  <Accordion title="Tool Calling (Function Calling)">
    <Tabs>
      <Tab title="Python SDK">
        ```python Python theme={null}
        tools = [{
          "type": "function",
          "function": {
            "name": "getWeather",
            "description": "Get the current weather",
            "parameters": {
              "type": "object",
              "properties": {
                "location": {"type": "string", "description": "City and state"},
                "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
              },
              "required": ["location"]
            }
          }
        }]

        response = client.chat.completions.create(
          model="grok-beta",
          messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "What's the weather like in Delhi - respond in JSON"}
          ],
          tools=tools,
          tool_choice="auto"
        )

        print(response.choices[0].finish_reason)
        ```
      </Tab>

      <Tab title="cURL">
        ```curl REST theme={null}
        curl -X POST "https://gateway.obiguard.ai/v1/chat/completions" \
          -H "Content-Type: application/json" \
          -H "x-obiguard-api-key: $OBIGUARD_API_KEY" \
          -d '{
            "model": "grok-beta",
            "messages": [
              {"role": "system", "content": "You are a helpful assistant."},
              {"role": "user", "content": "What'\''s the weather like in Delhi - respond in JSON"}
            ],
            "tools": [{
              "type": "function",
              "function": {
                "name": "getWeather",
                "description": "Get the current weather",
                "parameters": {
                  "type": "object",
                  "properties": {
                    "location": {"type": "string", "description": "City and state"},
                    "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
                  },
                  "required": ["location"]
                }
              }
            }],
            "tool_choice": "auto"
          }'
        ```
      </Tab>
    </Tabs>
  </Accordion>

  <Accordion title="Vision">
    Process images alongside text using xAI's vision capabilities:

    <Tabs>
      <Tab title="Python SDK">
        ```python Python theme={null}
        response = client.chat.completions.create(
          model="grok-beta",
          messages=[
            {
              "role": "user",
              "content": [
                {"type": "text", "text": "What's in this image?"},
                {
                  "type": "image_url",
                  "image_url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
                },
              ],
            }
          ],
          max_tokens=300,
        )

        print(response)
        ```
      </Tab>

      <Tab title="cURL">
        ```curl REST theme={null}
        curl -X POST "https://gateway.obiguard.ai/v1/chat/completions" \
          -H "Content-Type: application/json" \
          -H "Authorization: Bearer YOUR_Obiguard_API_KEY" \
          -d '{
            "model": "grok-beta",
            "messages": [
              {
                "role": "user",
                "content": [
                  {"type": "text", "text": "What'\''s in this image?"},
                  {
                    "type": "image_url",
                    "image_url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
                  }
                ]
              }
            ],
            "max_tokens": 300
          }'
        ```
      </Tab>
    </Tabs>
  </Accordion>

  <Accordion title="Embeddings">
    Generate embeddings for text using xAI's embedding models:

    `Coming Soon!`
  </Accordion>
</AccordionGroup>

## FAQs

<AccordionGroup>
  <Accordion title="How to get the xAI API key?">
    You can sign up to xAI [here](https://accounts.x.ai/) and grab your API key.
  </Accordion>

  <Accordion title="Is is free to use the xAI API key?">
    xAI typically gives some amount of free credits without you having to add your credit card. Reach out to their
    support team if you'd like additional free credits.
  </Accordion>

  <Accordion title="I am getting rate limited on xAI API">
    You can find your current rate limits imposed by xAI on the console.
  </Accordion>
</AccordionGroup>
