How to Get a Grok (xAI) API Key: A Step-by-Step Guide
On this page
This guide shows you how to get a Grok API key. You'll create an xAI account, generate your key, and make your first API call, plus a free alternative for adding Grok models to your web app.
Prerequisites
- An email address (or a Google account for single sign-on)
- A payment method — xAI's API is usage-based
- Basic familiarity with code (we'll show simple JavaScript examples)
Step 1: Create Your xAI Account
Open console.x.ai. This is the API console where you manage your keys, billing, and usage.
Click Sign in or Sign up and create your account using your email or Google SSO. Once you're in, you'll land on the dashboard.
Before your API key will work, you need to add a payment method. Go to the Billing page and add a credit card. xAI charges per token, and pricing varies by model. Set a monthly spending limit here to avoid surprises — you can always increase it later.
Step 2: Generate Your API Key
In the left sidebar, click API Keys, then click Create API Key.
Give your key a descriptive name like my-app-dev so you can identify it later. Advanced settings let you restrict which endpoints the key can access and set rate limits, but you can leave these at their defaults to start. Click Create API Key.
Important: Copy the key immediately. xAI only shows it once. If you lose it, you'll need to generate a new one.
Store it somewhere safe — a password manager, an .env file, or your platform's secrets manager. Never commit API keys to a public repository.
Step 3: Make Your First API Call
Now you're ready to use the key. Here's a quick example using the xAI JavaScript SDK:
npm install ai @ai-sdk/xai
import { createXai } from "@ai-sdk/xai";
import { generateText } from "ai";
const xai = createXai({ apiKey: process.env.XAI_API_KEY });
const { text } = await generateText({
model: xai("grok-4.5"),
prompt: "Hello, world!",
});
console.log(text);
If you get a response back, everything is working. For the full set of endpoints and parameters, see Grok's official API docs.
Can You Use the Grok API for Free?
Creating an API key is free, but using it requires paying per token. xAI's API has no permanent free tier and bills pay-as-you-go through the xAI Console. SuperGrok and X Premium are consumer plans for the Grok chat interface and the X app; they don't include API access, and the API doesn't require them.
Puter.js offers a different model. With its User-Pays model, you can add Grok models to your app for free: each user covers their own AI usage through their own Puter account, so your cost stays at $0 regardless of how many users you have, with no API key and no backend.
Add Grok models with the browser script tag:
<script src="https://js.puter.com/v2/"></script>
<script>
puter.ai.chat("Explain quantum computing in simple terms", {
model: "x-ai/grok-4.5"
}).then(response => {
document.body.innerHTML = response.message.content;
});
</script>
or the npm package:
import { puter } from '@heyputer/puter.js';
const response = await puter.ai.chat("Explain quantum computing in simple terms", {
model: "x-ai/grok-4.5"
});
console.log(response.message.content);
This fits front-end apps where your users sign into Puter: usage is tied to their accounts, and Puter.js runs in the browser, so it does not replace a server-side key for a backend you control. For a backend, see the pricing below.
How Much Does the Grok API Cost?
Grok bills per token, with separate input and output rates, so cost scales with usage instead of a flat fee. The flagship, Grok 4.5, is $2.00 per 1M input tokens and $6.00 per 1M output, with a 500K-token context window. The cheapest chat model, Grok Build 0.1, is $1.00 input and $2.00 output.
For the full breakdown, including the Batch and cached-input discounts and the per-invocation costs for server-side tools, see our Grok API pricing guide.
Conclusion
To get a Grok API key: sign up at console.x.ai, add a payment method, then go to API Keys and click Create API Key. From there you can make your first API call, or skip the key entirely and add Grok models to a web app for free with Puter.js.
Related
- How to Get an OpenAI API Key
- How to Get an Anthropic API Key
- How to Get a Gemini API Key
- How to Get a DeepSeek API Key
- How to Get an ElevenLabs API Key
- How to Get a Mistral API Key
- How to Get a MiniMax API Key
- How to Get a Moonshot AI (Kimi) API Key
- How to Get a Qwen API Key
- How to Get a Z.AI (GLM) API Key
- How to Get an OpenRouter API Key
- How to Get a FLUX (Black Forest Labs) API Key
- How to Get a Stability AI (Stable Diffusion) API Key
- How to Get a Kling API Key
- How to Get a Leonardo.Ai API Key
- How to Get a Together AI API Key
- How to Get a Cohere API Key
- Grok API Pricing
- Free, Unlimited Grok API
- How to Use OpenAI SDK with Puter
- Free, Unlimited OpenAI API
- Free, Unlimited Claude API
- Free, Unlimited Gemini API
- How to Use LangChain with Puter
- How to Use Vercel AI SDK with Puter
Ship a Full-Stack App with One Prompt
Build an AI chat app using Puter.js
Coding manually? see the guide