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

Free AI Index / 从零开始Getting started / Groq

FIRST API CALL / groq

Groq:跑通第一次 API 调用Make your first Groq 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 连通性未由本教程实测,先核对官方地区与使用条款。
账号
Groq Console 账号、API Key,以及当前模型列表中的可用模型 ID。
手机号
官方 Quickstart 未说明统一手机号要求;以注册页面提示为准。
实名认证
官方 Quickstart 未说明统一实名认证要求;以账号页面提示为准。
信用卡 / 付款方式
Free Plan 按模型限流;绑卡要求未从官方 Quickstart 确认。

这些条件会因账号、地区和产品计划而变化。标注“待核验”表示 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:GROQ_API_KEY = "<在此粘贴本地 API Key>"

macOS / LinuxmacOS / Linux

export GROQ_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.

Groq OpenAI 兼容 API

api

EndpointEndpoint
https://api.groq.com/openai/v1
认证Authentication
Authorization: Bearer API key
额度与限制Limits

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

前置条件Prerequisites

  • 已注册 Groq ConsoleGroq Console
  • 已创建 API keyAPI key
  • 已阅读当前 Free Plan LimitsFree Plan Limits

操作步骤Steps

  1. 创建密钥

    在 Groq Console 的 API Keys 页面创建密钥,并保存为 GROQ_API_KEY 环境变量。

  2. 确认模型与限制

    从官方 Models 页面选择当前可用模型,再按 Rate Limits 页面控制 RPM、RPD 和 TPM。

    操作Action:打开 https://console.groq.com/docs/models

  3. 发送测试请求

    使用官方支持的模型 ID 发起一次短请求。

    可复制命令Copyable command
    export GROQ_API_KEY="你的 Key"
    curl https://api.groq.com/openai/v1/chat/completions -H "Authorization: Bearer $GROQ_API_KEY" -H 'Content-Type: application/json' --data '{"model":"openai/gpt-oss-20b","messages":[{"role":"user","content":"Hello"}]}'

    预期结果Expected:返回 choices,并在超限时得到明确的 rate-limit 响应。

  4. Python SDK 示例

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

    可复制命令Copyable command
    python -m pip install -U openai
    
    import os
    from openai import OpenAI
    
    client = OpenAI(
        api_key=os.environ["GROQ_API_KEY"],
        base_url="https://api.groq.com/openai/v1",
    )
    response = client.chat.completions.create(
        model="openai/gpt-oss-20b",
        messages=[{"role": "user", "content": "用一句话解释 API 是什么。"}],
    )
    print(response.choices[0].message.content)

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

  5. Node.js SDK 示例

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

    可复制命令Copyable command
    npm install openai
    
    import OpenAI from "openai";
    
    const client = new OpenAI({
      apiKey: process.env.GROQ_API_KEY,
      baseURL: "https://api.groq.com/openai/v1",
    });
    const response = await client.chat.completions.create({
      model: "openai/gpt-oss-20b",
      messages: [{ role: "user", content: "用一句话解释 API 是什么。" }],
    });
    console.log(response.choices[0].message.content);

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

验证动作Validation

GET /openai/v1/models 返回 HTTP 200,并确认目标模型仍在官方模型列表。

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

官方来源Official sources