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

Use this quickstart to make your first call to the Bing Spell Check REST API. This simple Python application sends a request to the API and returns a list of words it didn't recognize, followed by suggested corrections. While this application is written in Python, the API is a RESTful Web service compatible with most programming languages. The source code for this application is available on GitHub.

Prerequisites

You must have a Cognitive Services API account with access to the Bing Spell Check API. If you don't have an Azure subscription, you can create an account for free. Before continuing, You will need the access key provided after activating your free trial, or a paid subscription key from your Azure dashboard.

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.

    '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 market, spell-check mode, and the text you want to check. Then create a string that appends the ?mkt= parameter to your market, and &mode= to your mode.

    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. 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, using response.on(). When a 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 () {
        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 end the request afterwards.

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

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