xAI Grok API key setup: permissions, rate limits and where the key lives

Create an xAI API key for Grok in console.x.ai, keep it on your server as XAI_API_KEY, grant only the models it needs, and know what the SpaceXSI name change means.

Simon, Founder of Shipyard

By · Founder of Shipyard

5 minute read

The short version

Sign in at console.x.ai, buy some API credits, open the API Keys page, create a key named after your product, and paste it into your host's environment variables as XAI_API_KEY. Call Grok only from server code, either with the OpenAI SDK pointed at https://api.x.ai/v1 or with xAI's own SDK. Give the key access to the models and endpoints your app actually uses, not everything, and set a per-key request limit so one bug cannot burn through your balance in an afternoon.

If you have seen the name SpaceXSI on the documentation and wondered whether you need a new account or a new key: you do not. xAI's developer docs now call the product the SpaceXSI API, but the console is still console.x.ai, requests still go to api.x.ai, and the documented variable is still XAI_API_KEY. Keys you already have keep working.

Keys belong to a team, and start with no permissions

Every xAI key is bound to a team and to the person who created it. xAI's own security FAQ warns that the API Keys page shows the keys of whichever team is selected, so if a key seems to be missing, check the team picker first. If you run more than one product, a team or at least a key per product keeps usage and limits apart, the same advice we give for Anthropic and OpenAI keys.

The part people miss is permissions. According to xAI's Management API reference, a key has no access to anything by default: you grant it endpoints and models, written as access rules such as api-key:model:grok-4.7. A wildcard rule gives a key every model and every endpoint, which is convenient and exactly what you do not want on a key that sits on a public server. If your app only does text, give the key the text endpoints and the one or two models you use. A leaked key that cannot call image or video generation is a much smaller problem.

Create the key, step by step

  1. Create an account at console.x.ai. It can be the same login you use for Grok, but xAI bills the API separately from Grok subscriptions, so add API credits in the console even if you already pay for Grok.
  2. Pick the team for this product in the team picker, then open API Keys and choose Create API Key. Name it after the product and the environment, for example myapp-prod.
  3. Grant only the endpoints and models the app calls. Set a requests per minute or tokens per minute limit on the key if the form offers it; the Management API documents qps, qpm and tpm limits and an expiry time per key.
  4. Copy the key once into your host as XAI_API_KEY. xAI shows the full key only at creation, so if you lose it, create a new one rather than hunting for the old value.
  5. Store the model name in a second variable such as XAI_MODEL, so moving to a newer Grok model is a settings change and not a deploy.

Rate limits and spend tiers

xAI limits each team per model in requests per second and tokens per minute, and the limits rise with your tier. Tiers depend on cumulative API spend since 1 January 2026 and never go down once reached. The table is from xAI's rate limit page today; check the Models page in the console for your own numbers.

Going over any limit returns HTTP 429, so retry with a growing wait rather than hammering the API. Note that cached prompt tokens still count toward tokens per minute even though they are billed at a lower rate.

TierCumulative spend neededgrok-4.7 requests per second
Tier 0$0150
Tier 1$50172
Tier 2$250208
Tier 3$1,000312
Tier 4$5,000500

Call Grok from the server

xAI's quickstart shows the OpenAI SDK working with only two changes: your xAI key and the base URL. That makes Grok the easiest provider to try if your app already talks to OpenAI. Keep the route behind a signed-in user and a per-user limit, and send a safety_identifier with each request, a hash of your own user ID, so that if one of your users breaks xAI's usage policy the report points at that user instead of your whole key.

TypeScript
// app/api/ask/route.ts
import OpenAI from 'openai'
import { createHash } from 'node:crypto'

const grok = new OpenAI({
  apiKey: process.env.XAI_API_KEY,
  baseURL: 'https://api.x.ai/v1',
})

export async function POST(req: Request) {
  // Check the session and a per-user rate limit here.
  const userId = 'from-your-session'
  const { question } = await req.json()
  const res = await grok.responses.create({
    model: process.env.XAI_MODEL!,
    input: question,
    safety_identifier: createHash('sha256').update(userId).digest('hex'),
  })
  return Response.json({ text: res.output_text })
}

Data, region and leaks

By default xAI keeps API requests and responses for 30 days for abuse checks and says it does not train on them. Zero Data Retention is available, but it is switched on for a whole team, not per key, and it turns off features such as the Batch API, Files and stored conversations, so most small apps should leave it off. If you need processing kept in the United States, xAI offers https://us.api.x.ai/v1, which currently serves fewer models at prices 10% above the global rate.

If a key leaks, open the console, select the right team, and disable or delete the key from the three-dot menu on the API Keys table, then create a new one and update your host. xAI is a GitHub secret scanning partner, so a key pushed to a public GitHub repository is usually disabled for you with an email. The honest weakness compared with OpenRouter is that the per-key controls are rate limits, not a dollar cap: a leaked key with a high limit can still spend your whole credit balance.

What Shipyard does here

Shipyard's AI step currently writes Anthropic, OpenAI or OpenRouter keys into your host, not xAI keys. If you want Grok today, you can add XAI_API_KEY to your host yourself after launch using the steps above, or choose OpenRouter as the provider, which lists Grok models behind one key with a per-key credit limit, and let Shipyard write that key on the server side only.

Sources

Launch in an hour, not a weekend

Shipyard connects your accounts once, then sets up the domain, hosting, database, email and payments for every new product.

Get started See how it works

Read next