Ship a Full-Stack App with One Prompt

Copy this prompt into your AI coding agent, or open it in one below.

Give this to your AI Create a to-do list app using Puter.js

Coding manually? see the guide

Tutorials

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.

xAI console sign-in page xAI API console 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.

xAI API keys page in the sidebar

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.

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 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.

Ship a Full-Stack App with One Prompt

Give this to your AI Build an AI chat app using Puter.js

Coding manually? see the guide