FREE AI INDEX / 统一产品界面我的收藏提交资源 ↗

Free AI Index / 从零开始Getting started / Zhipu BigModel

FIRST API CALL / zhipu-glm

Zhipu BigModel:跑通第一次 API 调用Make your first Zhipu BigModel API call

先确认注册与免费条件,再按步骤获取密钥、安装依赖并验证返回结果。教程只使用环境变量读取密钥。Check signup and free terms first, then create a key, install dependencies and validate the response. Examples read credentials from environment variables.

官方文档复核Official docs reviewed: 2026-10-07独立 API 实测Independent API test: 尚未记录Not recorded

注册与网络前提Signup and network requirements

网络 / 地区
中国区 API endpoint;本教程未做跨运营商连通性实测。
账号
BigModel 平台账号及 API Key;新用户资源包需先在账号内确认到账和有效期。
手机号
官方 API 文档未说明统一手机号要求;以注册页面提示为准。
实名认证
官方 API 文档未说明统一实名认证要求;以账号页面提示为准。
信用卡 / 付款方式
新用户体验包与付费资源要分别核对;绑卡要求未从 API 快速开始文档确认。

这些条件会因账号、地区和产品计划而变化。标注“待核验”表示 FreeLLM 尚未从当前链接的官方资料确认;不要据此推断为不需要。These requirements can vary by account, region and plan. “Unverified” means FreeLLM has not confirmed the condition from the linked official sources; it does not mean the requirement is absent.

获取并安全设置 API KeyGet and safely set your API key

在终端设置密钥Set the key in your terminal

以下命令只为当前终端会话设置环境变量。请在自己的电脑上把尖括号内容替换成真实密钥,不要把密钥发给 FreeLLM 或粘贴到公开代码。These commands set a variable for the current terminal session. Replace the angle-bracket text with your key on your own device; never send it to FreeLLM or commit it to source code.

Windows PowerShellWindows PowerShell

$env:ZHIPU_API_KEY = "<在此粘贴本地 API Key>"

macOS / LinuxmacOS / Linux

export ZHIPU_API_KEY="<在此粘贴本地 API Key>"

FreeLLM 资源记录FreeLLM resource records

详细操作步骤Detailed operation paths

每条路径都拆成前置条件、步骤、可复制命令和验证动作;免费条件仍以官方页面实时状态为准。curl 命令按 macOS / Linux shell 编写,Windows 用户可使用同页的 Python 或 Node.js 示例。Each path includes prerequisites, steps, copyable commands and a validation action; free terms still follow the provider's live official policy. The curl examples use a macOS/Linux shell; Windows users can run the Python or Node.js examples on this page.

使用新用户 2000 万 Tokens 体验包

api

EndpointEndpoint
https://open.bigmodel.cn/api/paas/v4/chat/completions
认证Authentication
Authorization: Bearer API key
额度与限制Limits

调用额度与限流按提供商当前控制台和官方文档为准;免费条件可能按模型、账号和地区变化。

前置条件Prerequisites

  • 智谱 BigModel 新用户账号BigModel
  • 账户已显示新用户体验资源包See official terms
  • 已创建 API KeyAPI Key

操作步骤Steps

  1. 确认资源包

    登录 BigModel 后确认 20,000,000 Tokens 新用户体验包已到账,并查看模型适用范围和到期日。

  2. 创建 API Key

    在 BigModel 控制台创建 API Key,并妥善保存。

  3. 发送最小请求

    使用标准 BigModel Chat Completions 接口发起最小请求,再检查资源包扣减。

    可复制命令Copyable command
    export ZHIPU_API_KEY="你的 Key"
    curl https://open.bigmodel.cn/api/paas/v4/chat/completions -H "Authorization: Bearer $ZHIPU_API_KEY" -H 'Content-Type: application/json' --data '{"model":"glm-5.2","messages":[{"role":"user","content":"你好"}]}'

    预期结果Expected:返回 chat completion;账户资源包页面显示对应消耗。

  4. Python SDK 示例

    安装 OpenAI SDK;在运行环境设置 ZHIPU_API_KEY,示例代码通过环境变量读取密钥。

    可复制命令Copyable command
    python -m pip install -U openai
    
    import os
    from openai import OpenAI
    
    client = OpenAI(
        api_key=os.environ["ZHIPU_API_KEY"],
        base_url="https://open.bigmodel.cn/api/paas/v4",
    )
    response = client.chat.completions.create(
        model="glm-5.2",
        messages=[{"role": "user", "content": "用一句话解释 API 是什么。"}],
    )
    print(response.choices[0].message.content)

    预期结果Expected:终端打印模型生成的回复;再到提供商控制台确认请求状态和额度扣减。

  5. Node.js SDK 示例

    安装 OpenAI SDK;在运行环境设置 ZHIPU_API_KEY,示例代码通过环境变量读取密钥。

    可复制命令Copyable command
    npm install openai
    
    import OpenAI from "openai";
    
    const client = new OpenAI({
      apiKey: process.env.ZHIPU_API_KEY,
      baseURL: "https://open.bigmodel.cn/api/paas/v4",
    });
    const response = await client.chat.completions.create({
      model: "glm-5.2",
      messages: [{ role: "user", content: "用一句话解释 API 是什么。" }],
    });
    console.log(response.choices[0].message.content);

    预期结果Expected:Node.js 打印模型生成的回复;再到提供商控制台确认请求状态和额度扣减。

验证动作Validation

请求成功,并确认实际扣减来自新用户体验资源包而非现金余额。

常见问题Common issues
  • 401:检查 API Key、环境变量和认证 Header。401: API Key、 Header。
  • 404:从官方模型清单复制当前模型 ID,并确认 endpoint 与账号地域一致。404: ID, endpoint 。
  • 429:查看当前限流和配额重置说明,降低请求频率后再重试。429: , 。

官方来源Official sources