Issue in accessing Bing Custom Web Search API v7

861 views Asked by At

I am trying to access the Bing Custom Search API. Custom Search Instance is setup and able to call API from BING portal (production tab), however when I am trying to access the same URL through JS. I am getting Failed Request as below

enter image description here

Below is the way I am accessing the API:

const query = "app";
const url = `https://api.bing.microsoft.com/v7.0/custom/search?q=${query}&customconfig=<CUSTOM_CONFIG_ID>&mkt=zh-CN`;
const option = {
  mode: "cors",
  headers: {
    "Ocp-Apim-Subscription-Key": <Subsription Key>
  }
};

fetch(url, option)
  .then((res) => res.json())
  .then((data) => console.log(data))
  .catch((err) => console.log(err));

I am getting error

TypeError: Failed to fetch

1

There are 1 answers

0
Jason Pan On
  1. Find CUSTOM_CONFIG_ID in https://www.customsearch.ai/ .

  2. Find Ocp-Apim-Subscription-Key on portal.

enter image description here

const fetch = require("node-fetch");

const query = "app";
const url = `https://api.bing.microsoft.com/v7.0/custom/search?q=${query}&customconfig=<CUSTOM_CONFIG_ID>&mkt=zh-CN`;
const option = {
    mode: "cors",
    headers: {
        "Ocp-Apim-Subscription-Key": '<Subsription Key>'
    }
};

fetch(url, option)
    .then((res) => res.json())
    .then((data) => console.log(data))
    .catch((err) => console.log("err: " + err));

My Test Result:

Please run node install node-fetch first.

enter image description here