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

如何在Node.js Express中为Bitcoin Average API配置x-ba-key请求头?

解决Bitcoin Average API请求头添加问题

Hey there! I see you're trying to add the required x-ba-key request header to your Bitcoin Average API calls using the request library—let me break down exactly where and how to implement this, plus fix a couple of small bugs in your existing code.

核心修改:添加请求头到options对象

The request library lets you specify custom headers directly in your options object using a headers property. This is where you'll include your x-ba-key value.

完整修正后的代码

Here's your updated code with the header added, plus fixes for variable confusion and undefined variables:

//TODO 1) require express, bodyparser & request
const express = require("express");
const bodyParser = require("body-parser");
const request = require("request");
//TODO 2) get the express
const app = express();
//TODO use bodyparser for getting the respnse from the local server
app.use(bodyParser.urlencoded({ extended: true }));
//TODO 3) specify the port for listening into the local server
app.listen(3000, function () {
  console.log("server is running at port 3000");
});
//TODO 4) sendFile - a file to the specific route
app.get("/", function (req, res) {
  res.sendFile(__dirname + "/index.html");
});
var theBitCoinPrice;
//TODO 5) post- use body parser for getting the response from the local server.
//TODO 6) request - use request function to use the API.
app.post("/", function (req, res) {
  console.log(req.body.fiat);
  var theFirstSymbol = req.body.crypto;
  var theSecondSymbol = req.body.fiat;
  var theAmount = req.body.amount;
  
  var options = {
    url: "https://apiv2.bitcoinaverage.com/convert/global",
    method: "GET",
    // Add your API key headers here
    headers: {
      'x-ba-key': 'NzI0MjM4Njc1NGQ3NDU4Mzg1NWU3YYmU4MTdiMaA' // Replace with your actual key
    },
    qs: {
      from: theFirstSymbol,
      to: theSecondSymbol,
      amount: theAmount,
    }
  }
  
  request(options, function (error, apiResponse, body) {
    // Fixed: Use `res` (Express response) instead of `apiResponse` (API's response)
    if (error) {
      return res.send("Error fetching data: " + error.message);
    }
    
    var data = JSON.parse(body);
    var price = data.price;
    var date = data.time;
    console.log(date);
    
    res.write("<h1>The current date is " + date + "</h1>");
    // Fixed: Use defined variables instead of undefined `crypto`/`fiat`
    res.write("The " + theAmount + " " + theFirstSymbol + " is " + price + " " + theSecondSymbol);
    res.send();
  });
});

关键说明

  • Header Placement: The headers object inside options is the correct spot to include your x-ba-key—the request library will automatically attach these headers to your GET request.
  • Variable Fix: Your original code used response.write but response refers to the API's response object, not Express's response to the client. We renamed it to apiResponse and use res (from the Express route handler) to send content back to the user.
  • Undefined Variables: The crypto and fiat variables in your res.write call weren't defined—we replaced them with theFirstSymbol and theSecondSymbol which you already declared.

安全提示

For production use, don't hardcode your API key in the code. Instead, use environment variables:

// In your code
headers: {
  'x-ba-key': process.env.BITCOIN_AVERAGE_API_KEY
}

Then set the variable in your terminal (or use a .env file with dotenv):

# Linux/macOS
export BITCOIN_AVERAGE_API_KEY="your-key-here"

# Windows (Command Prompt)
set BITCOIN_AVERAGE_API_KEY="your-key-here"

内容的提问来源于stack exchange,提问作者Aman Chaudhary

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 23:12:42