Quickstart: Check spelling with the Bing Spell Check REST API and Node.js

Warning

On October 30, 2020, the Bing Search APIs moved from Azure AI services to Bing Search Services. This documentation is provided for reference only. For updated documentation, see the Bing search API documentation. For instructions on creating new Azure resources for Bing search, see Create a Bing Search resource through the Azure Marketplace.

Use this quickstart to make your first call to the Bing Spell Check REST API. This simple JavaScript application sends a request to the API and returns a list of suggested corrections.

Although this application is written in JavaScript, the API is a RESTful Web service compatible with most programming languages. The source code for this application is available on GitHub.

Prerequisites

Create an Azure resource

Start using the Bing Spell Check API by creating one of the following Azure resources:

Bing Spell Check resource

  • Available through the Azure portal until you delete the resource.
  • Use the free pricing tier to try the service, and upgrade later to a paid tier for production.
  • The Bing Spell Check API is also offered in some tiers of the Bing Search v7 resource.

Multi-service resource

  • Available through the Azure portal until you delete the resource.
  • Use the same key and endpoint for your applications, across multiple Azure AI services.

Create and initialize a project

  1. Create a new JavaScript file in your favorite IDE or editor. Set the strictness, and require https. Then, create variables for your API endpoint's host, path, and your subscription key. You can use the global endpoint in the following code, or use the custom subdomain endpoint displayed in the Azure portal for your resource.

    'use strict';
    let https = require ('https');
    
    let host = 'api.cognitive.microsoft.com';
    let path = '/bing/v7.0/spellcheck';
    let key = '<ENTER-KEY-HERE>';
    
  2. Create variables for your search parameters and the text you want to check:

    1. Assign your market code to the mkt parameter with the = operator. The market code is the code of the country/region you make the request from.

    2. Add the mode parameter with the & operator, and then assign the spell-check mode. The mode can be either proof (catches most spelling/grammar errors) or spell (catches most spelling errors, but not as many grammar errors).

    let mkt = "en-US";
    let mode = "proof";
    let text = "Hollo, wrld!";
    let query_string = "?mkt=" + mkt + "&mode=" + mode;
    

Create the request parameters

Create your request parameters by creating a new object with a POST method. Add your path by appending your endpoint path, and query string. Then, add your subscription key to the Ocp-Apim-Subscription-Key header.

let request_params = {
   method : 'POST',
   hostname : host,
   path : path + query_string,
   headers : {
   'Content-Type' : 'application/x-www-form-urlencoded',
   'Content-Length' : text.length + 5,
      'Ocp-Apim-Subscription-Key' : key,
   }
};

Create a response handler

Create a function called response_handler to take the JSON response from the API, and print it. Create a variable for the response body. Append the response when a data flag is received by using response.on(). After an end flag is received, print the JSON body to the console.

let response_handler = function (response) {
    let body = '';
    response.on ('data', function (d) {
        body += d;
    });
    response.on ('end', function () {
        let body_ = JSON.parse (body);
        console.log (body_);
    });
    response.on ('error', function (e) {
        console.log ('Error: ' + e.message);
    });
};

Send the request

Call the API using https.request() with your request parameters and response handler. Write your text to the API, and then end the request.

let req = https.request (request_params, response_handler);
req.write ("text=" + text);
req.end ();

Run the application

  1. Build and run your project.

  2. If you're using the command line, use the following command to build and run the application:

    node <FILE_NAME>.js
    

Example JSON response

A successful response is returned in JSON, as shown in the following example:

{
   "_type": "SpellCheck",
   "flaggedTokens": [
      {
         "offset": 0,
         "token": "Hollo",
         "type": "UnknownToken",
         "suggestions": [
            {
               "suggestion": "Hello",
               "score": 0.9115257530801
            },
            {
               "suggestion": "Hollow",
               "score": 0.858039839213461
            },
            {
               "suggestion": "Hallo",
               "score": 0.597385084464481
            }
         ]
      },
      {
         "offset": 7,
         "token": "wrld",
         "type": "UnknownToken",
         "suggestions": [
            {
               "suggestion": "world",
               "score": 0.9115257530801
            }
         ]
      }
   ]
}

Next steps