Tutorials

How to Get a Grok (xAI) API Key: A Step-by-Step Guide

On this page

In this guide, you'll learn how to get your xAI API key for Grok. You'll create an xAI account, generate your key, and make your first API call. We'll also show you a simpler alternative if you want access to hundreds of AI models without managing multiple accounts.

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: Go to the xAI Console

Open console.x.ai. This is the API console where you manage your keys, billing, and usage.

xAI console landing page

Step 2: Create Your xAI Account

Click Sign in or Sign up and create your account using your email or Google SSO.

xAI console sign-in page

Once you're in, you'll land on the dashboard.

xAI API console dashboard

Before your API key will work, you need to add a payment method. Go to Billing in the left sidebar 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 3: Generate Your API Key

In the left sidebar, click API Keys, then click Create API Key.

xAI API keys page in the sidebar

You'll see a few options:

  • Name — give it something descriptive like my-app-dev so you can identify it later
  • Restrict access — choose which endpoints this key can access
  • Rate limits — set rate limits for the key

Click Create API Key.

xAI create new API key dialog

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.

xAI API key revealed with copy button

Step 4: 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"),
  prompt: "Hello, world!",
});

console.log(text);

If you get a response back, everything is working.

One API Key, Hundreds of Models

The process above works well for xAI specifically — but what happens when you want to use Claude, GPT, Gemini, Llama, Mistral, or any of the other major models?

You'd need to repeat the entire process for each provider: create an account, set up billing, generate and manage a separate key. That's a lot of overhead, especially if you're experimenting or building something that uses multiple models.

Puter offers a simpler approach: one account, one auth token, access to hundreds of models across providers.

Instead of managing five different API dashboards, you point your existing code to Puter's OpenAI-compatible endpoint and use your Puter auth token:

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.puter.com/puterai/openai/v1/",
  apiKey: "YOUR_PUTER_AUTH_TOKEN",
});

const response = await client.chat.completions.create({
  model: "grok-4", // or gpt-5-nano, claude-sonnet-4-6, gemini-2.5-flash-lite, etc.
  messages: [{ role: "user", content: "Hello, world!" }],
});

console.log(response.choices[0].message.content);

The code is almost identical — you're just pointing to a different URL and swapping the key. Switch between models by changing a single string.

To get your Puter auth token, create a free account at puter.com, then go to puter.com/dashboard and click Copy to grab your token.

Puter copy auth token

Conclusion

You now know how to create an xAI account, generate a Grok API key, and make your first API call. For a deeper dive, check out xAI's official API docs.

Free, Serverless AI and Cloud

Start creating powerful web applications with Puter.js in seconds!

Get Started Now

Read the Docs Try the Playground