Home › SpaceXSI API course: 3 beginner lessons
SpaceXSI API course: 3 beginner lessons
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
- Go to console.x.ai and create a SpaceXSI account.
- Add credits to the account (the API is paid by usage).
- Open the API Keys page and create a key.
- Store it as an environment variable named XAI_API_KEY, or in a .env file that you keep out of version control.
- 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..
- Create your account at console.x.ai and add credits.
- Create an API key on the API Keys page and run: export XAI_API_KEY="your_api_key"
- Install the OpenAI SDK for Python: pip install openai
- Create a client with base_url="https://api.x.ai/v1" and your key.
- Call client.responses.create(model="grok-4.7", input="…") and print response.output_text.
- Try the same request in the console Playground if you'd rather not write code yet.
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..
- Add tools=[{"type": "web_search"}] to a responses.create request so Grok can search and browse the web.
- Read the citations returned with the answer and show them to your users.
- For X posts instead of web pages, use the separate x_search tool.
- To get JSON back, set response_format to type "json_schema" and pass your schema (you can write it with Pydantic or Zod).
- Mark which fields are required; fields not listed are optional, and additionalProperties defaults to false.
- Parse the reply in your code and handle the case where a value is null.
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..
- Call client.images.generate(model="grok-imagine-image-2.0", prompt="…") with the OpenAI SDK pointed at https://api.x.ai/v1.
- Read the image URL from response.data[0].url and download it straight away: URLs are temporary.
- Ask for base64 output instead if you want to embed the image directly.
- Set n (1 to 10) to get several versions in one request, and set the aspect ratio for where the image will be used.
- Use the editing guide to change an existing image, or the video guides for image-to-video.
Sources
Also: All SpaceXSI products · Grok app course · All tutorials