How to Get a Z.AI (GLM) API Key: A Step-by-Step Guide
On this page
This guide shows you how to get a Z.AI (GLM) API key. You'll create a Z.AI account, generate your key, and make your first API call, plus a free alternative for adding GLM models to your web app.
Prerequisites
- An email or Google account
- Basic familiarity with code (we'll show simple JavaScript examples)
Step 1: Create Your Z.AI Account
Go to z.ai/chat. You'll see the home screen.
Click the Login button at the top right, and complete the login or sign-up with your email or Google account.
Step 2: Generate Your API Key
Once logged in, you'll be sent back to the home screen. Click your profile menu at the top right and select API Keys.
You'll land on the API keys page.
Click Create a new API key. Enter a name for your key.
Once created, you'll be able to copy your API key.
Unlike some other providers, Z.AI lets you come back to the API keys page and copy your key again at any time.
Store it somewhere safe — a password manager, an .env file, or your platform's secrets manager. Never commit API keys to a public repository.
Step 3: Make Your First API Call
Z.AI uses an OpenAI-compatible API, so you can use the OpenAI SDK directly. Install it first:
npm install openai
Then make your first call:
import OpenAI from "openai";
const openai = new OpenAI({
baseURL: "https://api.z.ai/api/paas/v4",
apiKey: process.env.ZAI_API_KEY,
});
const completion = await openai.chat.completions.create({
model: "glm-5.2",
messages: [{ role: "user", content: "Hello, world!" }],
});
console.log(completion.choices[0].message.content);
If you get a response back, everything is working. For the full set of endpoints and parameters, see Z.AI's official API docs.
Can You Use the Z.AI (GLM) API for Free?
Creating an API key is free, but using it requires paying per token. Three GLM models are free on the API: GLM-4.7-Flash and GLM-4.5-Flash for text, and GLM-4.6V-Flash for vision. These are genuinely free rather than trial credits, though they are rate-limited and tuned for speed over depth.
Puter.js offers a different model. With its User-Pays model, you can add Z.AI (GLM) 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 Z.AI (GLM) 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: "z-ai/glm-5.2"
}).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: "z-ai/glm-5.2"
});
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 Z.AI (GLM) API Cost?
GLM-5.2, the current flagship, costs $1.40 per 1M input tokens and $4.40 per 1M output tokens. The lowest paid input rate is GLM-4.7-FlashX at $0.07 input and $0.40 output, and two text models (GLM-4.7-Flash and GLM-4.5-Flash) are free.
For the full breakdown, see our Z.AI (GLM) API pricing guide.
Why Isn't My Key Working?
Most first-call failures trace back to a few errors:
- 401, 1000 ("Authentication Failed"): the key is wrong, was revoked, or picked up a stray space or newline when copied. Regenerate it and paste again.
- 401, 1003 ("Authentication Token expired"): the token is no longer valid, so generate a fresh one from your API keys page.
- 429, 1113 ("Insufficient balance or no resource package"): the account is out of credit. Recharge your balance, then retry.
- 429, 1302 ("Rate limit reached for requests"): requests are going out faster than your plan allows. Add retries with exponential backoff, or slow your request rate.
- 400, 1211 ("Unknown Model"): the model code is wrong or not available to your account, so check the spelling against the model list.
Most of these come back to billing, so check your balance first. For the full list of status and error codes, see Z.AI (GLM)'s error codes reference.
Conclusion
To get a Z.AI (GLM) API key: sign up at z.ai/chat, then open your profile menu, select API Keys, and click Create a new API key. From there you can make your first API call, or skip the key entirely and add GLM models to a web app for free with Puter.js.
Related
- Free, Unlimited Z.AI (GLM) API
- Z.ai GLM API Pricing
- Free, Unlimited Claude API
- How to Get an OpenAI API Key
- How to Get an Anthropic (Claude) API Key
- How to Get a Gemini API Key
- How to Get a Grok (xAI) API Key
- How to Get a DeepSeek API Key
- How to Get an ElevenLabs API Key
- How to Get a Mistral API Key
- How to Get a MiniMax API Key
- How to Get a Moonshot AI (Kimi) API Key
- How to Get an OpenRouter API Key
- How to Get a FLUX (Black Forest Labs) API Key
- How to Get a Stability AI (Stable Diffusion) API Key
- How to Get a Kling API Key
- How to Get a Leonardo.Ai API Key
- How to Get a Together AI API Key
- How to Get a Cohere API Key
- How to Get a Qwen API Key
- How to Use OpenAI SDK with Puter
- Free, Unlimited OpenAI API
- Free, Unlimited Gemini API
- How to Use LangChain with Puter
- How to Use Vercel AI SDK with Puter
Ship a Full-Stack App with One Prompt
Build an AI chat app using Puter.js
Coding manually? see the guide