Modifier

Partager via


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

Use this quickstart to make your first call to 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 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.bing.microsoft.com';
    let path = '/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