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

# Grok-2-Image - Text To Image - openai

> Grok 2 image text to image generation. Create bold, creative, and high-quality AI visuals with a unique edge from your prompts.

## Authentication

1. Sign up for a GPTProto account at [https://gptproto.com](https://gptproto.com)
2. Navigate to the API Keys section in your dashboard
3. Generate a new API key (sk-xxxxx)
4. Copy and securely store your API key
   For authentication details, please refer to the [Authentication](https://docs.gptproto.com/authentication) section.

## Initiate Request

<CodeGroup>
  ```bash cURL theme={null}
  curl --location 'https://gptproto.com/v1/images/generations' \
  --header 'Authorization: GPTPROTO_API_KEY' \
  --header 'Content-Type: application/json' \
  --data '{
    "model": "grok-2-image",
    "prompt": "a cat.",
    "n": 1
  }'

  ```

  ```javascript JavaScript theme={null}
  const axios = require('axios');
  let data = JSON.stringify({
    "model": "grok-2-image",
    "prompt": "a cat.",
    "n": 1
  });

  let config = {
    method: 'post',
    maxBodyLength: Infinity,
    url: 'https://gptproto.com/v1/images/generations',
    headers: { 
      'Authorization': 'GPTPROTO_API_KEY', 
      'Content-Type': 'application/json'
    },
    data : data
  };

  axios.request(config)
  .then((response) => {
    console.log(JSON.stringify(response.data));
  })
  .catch((error) => {
    console.log(error);
  });


  ```

  ```python Python theme={null}
  import requests
  import json

  url = "https://gptproto.com/v1/images/generations"

  payload = json.dumps({
    "model": "grok-2-image",
    "prompt": "a cat.",
    "n": 1
  })
  headers = {
    'Authorization': 'GPTPROTO_API_KEY',
    'Content-Type': 'application/json'
  }

  response = requests.request("POST", url, headers=headers, data=payload)

  print(response.text)


  ```

  ```go Go theme={null}
  package main

  import (
    "fmt"
    "strings"
    "net/http"
    "io"
  )

  func main() {

    url := "https://gptproto.com/v1/images/generations"
    method := "POST"

    payload := strings.NewReader(`{
    "model": "grok-2-image",
    "prompt": "a cat.",
    "n": 1
  }`)

    client := &http.Client {
    }
    req, err := http.NewRequest(method, url, payload)

    if err != nil {
      fmt.Println(err)
      return
    }
    req.Header.Add("Authorization", "GPTPROTO_API_KEY")
    req.Header.Add("Content-Type", "application/json")

    res, err := client.Do(req)
    if err != nil {
      fmt.Println(err)
      return
    }
    defer res.Body.Close()

    body, err := io.ReadAll(res.Body)
    if err != nil {
      fmt.Println(err)
      return
    }
    fmt.Println(string(body))
  }

  ```
</CodeGroup>

<Accordion title="Response Example">
  ```json theme={null}
  {
      "data": [
          {
              "url": "https://imgen.x.ai/xai-imgen/xai-tmp-imgen-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.jpeg",
              "revised_prompt": "a photograph of a cat wearing a hat, sitting on a wooden surface with a neutral expression. The cat has a mix of gray and white fur, with its front paws resting on the hat, which has a wide brim and a dark color, possibly black or dark gray. The background features a blurred indoor setting with a wooden wall and a hint of a window with natural light filtering through. The lighting is soft and natural, suggesting an indoor environment with ample sunlight. The cat's expression is neutral, with its eyes looking directly at the camera, and its ears are pointed upwards. The hat appears to be made of a soft material, possibly felt, and it covers the top of the cat's head while leaving its face visible. The image has a shallow depth of field, focusing sharply on the cat and the hat while blurring the background. There are no texts visible in the image. The cat's posture is relaxed, with its body slightly turned to the side and its tail not visible in the frame. The overall composition is balanced, with the cat and hat as the central elements, and the background includes a wooden structure that could be part of furniture or a wall."
          }
      ]
  }
  ```
</Accordion>

## Parameters

### Core Parameters

| Parameter         | Type           | Required | Default | Range                                                                                                                                                           | Description                                                                                                                                                                                     |
| ----------------- | -------------- | -------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `model`           | string         | ✅ Yes    | -       | -                                                                                                                                                               | Model to be used.                                                                                                                                                                               |
| `prompt`          | string         | ✅ Yes    | -       | -                                                                                                                                                               | Prompt for image generation.                                                                                                                                                                    |
| `image`           | string or file | ❌ No     | -       | -                                                                                                                                                               | base64-encoded data string of the picture or a public URL.                                                                                                                                      |
| `n`               | integer        | ❌ No     | `1`     | `1-10`                                                                                                                                                          | The number of images to generate. Must be between 1 and 10.                                                                                                                                     |
| `aspect_ratio`    | string         | ❌ No     | `1:1`   | `1:1`<br />`3:4`<br />`4:3`<br />`9:16`<br />`16:9`<br />`2:3`<br />`3:2`<br />`9:19.5`<br />`19.5:9`<br />`9:20`<br />`20:9`<br />`1:2`<br />`2:1`<br />`auto` | Aspect ratio of the generated image. Only supported by grok-imagine models.                                                                                                                     |
| `response_format` | string         | ❌ No     | `url`   | `b64_json`<br />`url`                                                                                                                                           | Response format to return the image in. Can be url or b64\_json. If b64\_json is specified, the image will be returned as a base64-encoded string instead of a url to the generated image file. |

## Error Codes

### Common Error Codes

| Error Code | Error Name               | Description                                                                                                       |
| ---------- | ------------------------ | ----------------------------------------------------------------------------------------------------------------- |
| 401        | Unauthorized             | API key is missing or invalid                                                                                     |
| 403        | Forbidden                | Your API key doesn't have permission to access this resource, or insufficient balance for the requested operation |
| 429        | Too Many Requests        | You've exceeded your rate limit                                                                                   |
| 500        | Internal server error    | An internal server error occurred                                                                                 |
| 503        | Content policy violation | Content blocked due to safety concerns (actual status code is 400)                                                |
