Home › SpaceXSI API course: 3 beginner lessons

SpaceXSI API course: 3 beginner lessons

Last checked

What it does

The SpaceXSI API lets your own code use Grok models for text and code, images and video (Imagine), voice, and live web and X search, billed by usage.

Who it's for: Developers who want Grok models inside their own app, script or product.

Made by SpaceXSI. SpaceXSI's own docs describe Grok as “SpaceXSI's assistant”.

Sign up and log in
  1. Go to console.x.ai and create a SpaceXSI account.
  2. Add credits to the account (the API is paid by usage).
  3. Open the API Keys page and create a key.
  4. Store it as an environment variable named XAI_API_KEY, or in a .env file that you keep out of version control.
  5. Install an SDK: the xAI SDK (pip install xai-sdk), the OpenAI SDK (pip install openai or npm install openai) or the Vercel AI SDK (npm install ai @ai-sdk/xai zod).

Text model grok-4.7: US$2.00 per million input tokens and US$6.00 per million output tokens (prompts under 200k tokens). Images from US$0.02 each. All API prices are listed in USD on the docs pricing page.

Lesson 1: Get a key and make your first request

The API is reached at https://api.x.ai/v1 and works with the OpenAI and Vercel AI SDKs as well as SpaceXSI's own xAI SDK, so you only change the base URL and the key..

  1. Create your account at console.x.ai and add credits.
  2. Create an API key on the API Keys page and run: export XAI_API_KEY="your_api_key"
  3. Install the OpenAI SDK for Python: pip install openai
  4. Create a client with base_url="https://api.x.ai/v1" and your key.
  5. Call client.responses.create(model="grok-4.7", input="…") and print response.output_text.
  6. Try the same request in the console Playground if you'd rather not write code yet.

Open lesson 1 →

Lesson 2: Live answers with web search and JSON output

Two features turn a chat model into something an app can rely on: the web_search tool lets Grok look things up now, and Structured Outputs make it reply in a JSON shape your code can read..

  1. Add tools=[{"type": "web_search"}] to a responses.create request so Grok can search and browse the web.
  2. Read the citations returned with the answer and show them to your users.
  3. For X posts instead of web pages, use the separate x_search tool.
  4. To get JSON back, set response_format to type "json_schema" and pass your schema (you can write it with Pydantic or Zod).
  5. Mark which fields are required; fields not listed are optional, and additionalProperties defaults to false.
  6. Parse the reply in your code and handle the case where a value is null.

Open lesson 2 →

Lesson 3: Generate images with the Imagine API

The Imagine API creates and edits images (and video) from your prompts, for things like product shots, mock-ups and social graphics, from the same key you already have..

  1. Call client.images.generate(model="grok-imagine-image-2.0", prompt="…") with the OpenAI SDK pointed at https://api.x.ai/v1.
  2. Read the image URL from response.data[0].url and download it straight away: URLs are temporary.
  3. Ask for base64 output instead if you want to embed the image directly.
  4. Set n (1 to 10) to get several versions in one request, and set the aspect ratio for where the image will be used.
  5. Use the editing guide to change an existing image, or the video guides for image-to-video.

Open lesson 3 →

Sources

Start Lesson 1 →

Also: All SpaceXSI products · Grok app course · All tutorials

Report an error on this page