Unified.to
All articles

How to Get a Grok (xAI) and Groq API Key — and Connect It to Your Product

·

To get a Grok API key, sign up at console.x.ai, buy prepaid API credits under Billing, and create a key on the API Keys page. Then send it as a Bearer token to https://api.x.ai/v1. As of 4 November 2026, xAI recommends grok-4.7 for most work, and the Grok API has no free tier: you pay per token from prepaid credits.

Looking for Groq (with a q), the LPU inference platform? That's a different company with a different key: see How to Get a Groq API Key and Connect It to Your Product.

What is a Grok API key?

A Grok API key is the credential the xAI API uses to authenticate requests to Grok models. You create it in the xAI Console, and it belongs to a team: usage and billing are tracked per team, not per person. Grok is xAI's model family, the same models behind grok.com and the Grok assistant on X.

xAI is now part of SpaceX. SpaceX acquired xAI in February 2026 and rebranded it SpaceXAI in July 2026, and xAI's developer docs now refer to the company as SpaceXSI. The API, the Console and the docs still run on x.ai domains: api.x.ai, console.x.ai and docs.x.ai (checked 4 November 2026).

How to get a Grok API key

  1. Create an account at https://console.x.ai.
  2. Add credits. Open Billing and buy prepaid API credits. Guest Checkout is currently the only purchase method; bank transfers take 2–3 business days to credit. If you have a promo code, enter it on the same page.
  3. Create the key. Open API Keys (console.x.ai/team/default/api-keys) and click Create API Key. Name it for where it will run (prod-backend, ci-pipeline).
  4. Restrict it. Choose which endpoints and models the key can call. The same restrictions exist as ACLs in xAI's Management API: api-key:endpoint:chat, api-key:endpoint:image and api-key:model:<model name>, or * for all.
  5. Copy the key when it's shown and store it in a secrets manager.

How do you store and test a Grok API key?

xAI's SDKs and examples read the key from the XAI_API_KEY environment variable:

export XAI_API_KEY="your-xai-api-key"

Use XAI_API_KEY, not GROK_API_KEY. In production, keep the key in a secrets manager (AWS Secrets Manager, HashiCorp Vault, GCP Secret Manager), never in Git or chat.

Test it with the Responses API, which xAI recommends over Chat Completions:

curl https://api.x.ai/v1/responses \
  -H "Authorization: Bearer $XAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "grok-4.7",
    "input": "Reply with the word ok."
  }'

A JSON response with Grok's output means the key works. The API base URL is api.x.ai/v1; console.x.ai is the dashboard and grok.com is the consumer app.

Is the Grok API free?

No. As of 4 November 2026, xAI's pricing and billing pages describe per-token prices paid from prepaid credits or monthly invoicing, and no free tier. API credits are non-refundable.

xAI's API docs don't say that a SuperGrok or X subscription includes API credits. API keys and API billing live in the xAI Console, under your team.

How much does the Grok API cost?

Prices per 1M tokens on xAI's pricing page (last updated 10 October 2026):

ModelInputCached inputOutput
grok-4.7 (under 200K context)$2.00$0.50$6.00
grok-4.7 (200K context or more)$4.00$1.00$12.00
grok-4.3 (under 200K context)$1.25$0.20$2.50
grok-4.3 (200K context or more)$2.50$0.40$5.00
Server-side tools are billed on top of tokens:
ToolPrice
Web Search (web_search)$5 per 1,000 calls
X Search (x_search)$5 per 1,000 posts, $10 per 1,000 profiles
Code Execution (code_execution)$5 per 1,000 calls
Collections Search (collections_search)$2.50 per 1,000 calls
File Attachments (attachment_search)$5 per 1,000 calls
Check xAI's pricing page before you estimate costs; prices change.

How do you control Grok API spending?

Set these under Billing in the xAI Console before you go to production:

  • Invoiced billing limit. Monthly invoiced billing is off by default, with a $0 limit. At $0, the API spends only prepaid credits and rejects requests once they run out. If you turn invoicing on, usage beyond your credits is billed at month end up to the limit you set, and requests stop when the limit is reached.
  • Usage warning. The Console warns you at 80% of the monthly limit you set.
  • Auto top-up. Buys more credits when your balance drops below a threshold you choose, with a minimum top-up of $5 and a monthly maximum.

Which Grok model should you use?

Use grok-4.7. As of 4 November 2026, xAI's Models page (last updated 21 September 2026) says to use Grok 4.7 for everything except image, video and voice, including code. It has a 500K-token context window.

xAI retired these model IDs on 15 May 2026: grok-4-1-fast-reasoning, grok-4-1-fast-non-reasoning, grok-4-fast-reasoning, grok-4-fast-non-reasoning, grok-4-0709, grok-code-fast-1 and grok-3. Calls to them still work, but they're redirected to grok-4.3 (at low reasoning effort for the reasoning models, none for the others) and billed at grok-4.3 rates. If your code still names one of them, set the model explicitly so you choose the model and reasoning effort you pay for.

Grok Responses API or Chat Completions?

Use the Responses API (POST /v1/responses). xAI's docs call it the recommended interface and label Chat Completions deprecated, a legacy endpoint with limited updates. Server-side tools (web search, X search, code execution, remote MCP), stored conversations through previous_response_id, and reasoning content are available only on the Responses API.

How do you get real-time X and web data from Grok?

Add server-side tools to the tools array of a Responses API request. There's no boolean flag such as enable_x_context. x_search searches posts and profiles on X, and web_search searches the web, each billed per use at the prices above.

How do you switch from OpenAI to Grok?

The xAI API works with the OpenAI SDK. Change the API key and the base URL:

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["XAI_API_KEY"],
    base_url="https://api.x.ai/v1",
)

response = client.responses.create(model="grok-4.7", input="Hello, Grok")
print(response.output_text)

What are the Grok API rate limits?

xAI sets per-model request-rate (RPS) and token-rate (TPM) limits for each team. They rise with your tier, which depends on cumulative spend since 1 January 2026:

TierSpend since 1 Jan 2026
Tier 0$0 (default)
Tier 1$50
Tier 2$250
Tier 3$1,000
Tier 4$5,000
EnterpriseOn request
Spend counts prepaid credit purchases and paid invoices. Tiers upgrade automatically and never downgrade. Your team's tier and exact limits are on the Models page in the xAI Console. Exceeding a limit returns 429.

How do you fix common Grok API errors?

  • 400 Bad Request: an invalid argument in the body or URL. xAI also returns 400 for an incorrect API key.
  • 401 Unauthorized: the Authorization: Bearer $XAI_API_KEY header is missing or the key is invalid. Check for trailing whitespace, or create a new key.
  • 403 Forbidden: the key or team lacks permission, or the key is blocked. Check the key's endpoint and model restrictions, or ask your team admin.
  • 404 Not Found: the model ID doesn't exist or the endpoint URL is misspelled. Check the model against the Models page.
  • 415 Unsupported Media Type: the body is empty or Content-Type: application/json is missing.
  • 422 Unprocessable Entity: a field in the body has an invalid format.
  • 429 Too Many Requests: you've hit your tier's rate limit. Back off exponentially and retry, or raise your limits in the Console.
  • Requests rejected with no credits left: buy credits, or enable invoiced billing with a limit (see spending controls above).

Service status is at status.x.ai.

Using Grok through Unified.to

Unified.to's Generative AI API puts X.ai Grok behind the same request shape as its other AI integrations. You integrate once and switch providers by changing the connection ID, not your application code.

As of 2 November 2026, Unified.to's X.ai Grok integration supports four objects:

  • Model: list and get models, with max tokens and temperature support
  • Prompt: create a prompt with messages, temperature and max tokens; returns responses and tokens used
  • Embedding: create embeddings
  • Skill: list, get and remove Agent Skills

A Prompt also accepts mcp_url, a remote MCP URL that is sent to the LLM API so the model can call that server's tools, and skill_ids to give the model Agent Skills.

Letting Grok act on your customers' SaaS data with Unified MCP

Unified.to's MCP server exposes a customer's connection as tools: reading CRM deals, fetching files, updating records. Server URLs by region:

  • US: https://mcp-api.unified.to/mcp
  • EU: https://mcp-api-eu.unified.to/mcp
  • AU: https://api-au.unified.to/mcp

For function calling with Grok:

  1. Fetch the tools in Grok's format: GET /tools?type=grok. Each tool comes back as { type: "function", function: { name, description, parameters } }.
  2. Pass them in your xAI request.
  3. When Grok returns a tool call, run it with POST /tools/{id}/call, with the arguments as the JSON body.
  4. Send the result back to Grok as a tool output.

Control what the model can reach with these URL parameters:

  • permissions: only allow tools covered by these Unified.to permissions (for example crm_contact_read)
  • tools: limit the tool list, with wildcards (list_*)
  • hide_sensitive=true: remove PII fields such as names, emails and phone numbers from tool results

Each tool call counts as one API request on your Unified.to plan.

Start your 30-day free trial · Book a demo

FAQ

Do I need X Premium or SuperGrok to use the Grok API? No. You create API keys and buy API credits in the xAI Console at console.x.ai. xAI's API docs don't say that a consumer subscription includes API credits.

Why does my Grok API key return 401? The key is missing from the Authorization: Bearer header, copied with whitespace, or deleted. Create a new key in the Console if needed.

What's the difference between Grok and Groq? Grok (with a k) is xAI's model family, called through api.x.ai. Groq (with a q) is a separate company that runs open-weight models on its own LPU hardware, called through api.groq.com. They have separate keys and separate billing. See How to Get a Groq API Key.

All articles