Skip to content

cURL 示例

完整的 cURL 使用示例,帮助你快速上手 Next API。

环境准备

安装 cURL

大多数系统已预装 cURL。验证安装:

bash
curl --version

设置环境变量

bash
export API_KEY="sk-你的密钥"
export BASE_URL="https://api.nextapi.pro/v1"

基础用法

简单对话

bash
curl https://api.nextapi.pro/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "gpt-4o-mini",
    "messages": [
      {
        "role": "user",
        "content": "你好!请介绍一下你自己。"
      }
    ]
  }'

多轮对话

bash
curl https://api.nextapi.pro/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "gpt-4o-mini",
    "messages": [
      {
        "role": "system",
        "content": "你是一个友好的助手。"
      },
      {
        "role": "user",
        "content": "你好!"
      },
      {
        "role": "assistant",
        "content": "你好!有什么可以帮助你的吗?"
      },
      {
        "role": "user",
        "content": "请介绍一下 cURL。"
      }
    ]
  }'

高级用法

流式响应

bash
curl https://api.nextapi.pro/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "gpt-4o-mini",
    "messages": [
      {
        "role": "user",
        "content": "写一首关于春天的诗"
      }
    ],
    "stream": true
  }'

控制参数

bash
curl https://api.nextapi.pro/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "gpt-4o-mini",
    "messages": [
      {
        "role": "user",
        "content": "给我讲个笑话"
      }
    ],
    "temperature": 0.7,
    "max_tokens": 100,
    "top_p": 0.9,
    "frequency_penalty": 0.5,
    "presence_penalty": 0.5
  }'

图片理解

bash
curl https://api.nextapi.pro/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "gpt-4o",
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "text",
            "text": "这张图片里有什么?"
          },
          {
            "type": "image_url",
            "image_url": {
              "url": "https://example.com/image.jpg"
            }
          }
        ]
      }
    ],
    "max_tokens": 300
  }'

函数调用

bash
curl https://api.nextapi.pro/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "gpt-4o-mini",
    "messages": [
      {
        "role": "user",
        "content": "北京今天天气怎么样?"
      }
    ],
    "tools": [
      {
        "type": "function",
        "function": {
          "name": "get_weather",
          "description": "获取指定城市的天气信息",
          "parameters": {
            "type": "object",
            "properties": {
              "city": {
                "type": "string",
                "description": "城市名称,例如:北京"
              }
            },
            "required": ["city"]
          }
        }
      }
    ],
    "tool_choice": "auto"
  }'

错误处理

检查响应状态

bash
response=$(curl -s -w "\n%{http_code}" https://api.nextapi.pro/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "gpt-4o-mini",
    "messages": [{"role": "user", "content": "你好"}]
  }')

http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')

if [ "$http_code" -eq 200 ]; then
  echo "成功: $body"
else
  echo "错误 (HTTP $http_code): $body"
fi

重试机制

bash
#!/bin/bash

max_retries=3
retry_delay=2

for i in $(seq 1 $max_retries); do
  response=$(curl -s -w "\n%{http_code}" https://api.nextapi.pro/v1/chat/completions \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $API_KEY" \
    -d '{
      "model": "gpt-4o-mini",
      "messages": [{"role": "user", "content": "你好"}]
    }')
  
  http_code=$(echo "$response" | tail -n1)
  
  if [ "$http_code" -eq 200 ]; then
    body=$(echo "$response" | sed '$d')
    echo "成功: $body"
    exit 0
  elif [ "$http_code" -eq 429 ]; then
    echo "遇到频率限制,等待 $retry_delay 秒后重试..."
    sleep $retry_delay
    retry_delay=$((retry_delay * 2))
  else
    echo "错误 (HTTP $http_code)"
    exit 1
  fi
done

echo "重试次数已达上限"
exit 1

实用脚本

格式化输出

bash
curl -s https://api.nextapi.pro/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "gpt-4o-mini",
    "messages": [{"role": "user", "content": "你好"}]
  }' | jq '.choices[0].message.content'

保存对话历史

bash
#!/bin/bash

# 初始化对话历史
history='[]'

# 添加用户消息
user_message="你好"
history=$(echo "$history" | jq --arg msg "$user_message" '. + [{"role": "user", "content": $msg}]')

# 发送请求
response=$(curl -s https://api.nextapi.pro/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d "{
    \"model\": \"gpt-4o-mini\",
    \"messages\": $history
  }")

# 提取助手回复
assistant_message=$(echo "$response" | jq -r '.choices[0].message.content')

# 添加助手消息到历史
history=$(echo "$history" | jq --arg msg "$assistant_message" '. + [{"role": "assistant", "content": $msg}]')

# 输出
echo "助手: $assistant_message"
echo "历史: $history"

下一步