You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Coinbase Commerce API报400错误 提示缺失pricing_type参数排查

问题产生原因

构造POST请求体时错误地将所有业务参数嵌套在了query对象内部,Coinbase Commerce创建charge接口要求所有必填参数直接放在请求体的最外层,不接受额外的嵌套包装层,因此接口无法读取到顶层的pricing_type字段,触发参数缺失报错。

错误结构验证

当前代码序列化后实际发送的请求体结构如下:

{
  "query": {
    "name": "The Sovereign Individual",
    "description": "Mastering the Transition to the Information Age",
    "local_price": {
      "amount": "100.00",
      "currency": "USD"
    },
    "pricing_type": "fixed_price",
    "metadata": {
      "customer_id": "id_1005",
      "customer_name": "Satoshi Nakamoto"
    },
    "redirect_url": "https://charge/completed/page",
    "cancel_url": "https://charge/canceled/page"
  }
}

接口校验时只会遍历请求体的顶层字段,该结构下顶层只有query字段,没有pricing_type,因此返回Required parameter missing: pricing_type错误,和你代码里有没有在内部对象写这个字段没有关系。

解决方法

移除请求体序列化时多余的query嵌套层,直接将构造好的charge参数对象序列化为请求体即可,修正后的代码如下:

const chargeParams = {
  name: 'The Sovereign Individual',
  description: 'Mastering the Transition to the Information Age',
  local_price: {
    amount: '100.00',
    currency: 'USD',
  },
  pricing_type: 'fixed_price',
  metadata: {
    customer_id: 'id_1005',
    customer_name: 'Satoshi Nakamoto',
  },
  redirect_url: 'https://charge/completed/page',
  cancel_url: 'https://charge/canceled/page',
};
const opts = {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'X-CC-Api-Key': 'api_key',
    'X-CC-Version': '2018-03-22',
  },
  body: JSON.stringify(chargeParams),
};

fetch('https://api.commerce.coinbase.com/charges', opts)
  .then(res => res.json())
  .then(jsonRes => console.log(jsonRes));
补充说明
  • 修复后请求体顶层会直接包含pricing_type等所有接口要求的参数,可直接通过参数校验
  • 测试调用时请将X-CC-Api-Key字段替换为你自己的有效API密钥,避免出现鉴权失败问题

内容的提问来源于stack exchange,提问作者Ifeoluwa Alao

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.27 23:18:19