Quickstart: Check spelling with the Bing Spell Check REST API and Python

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 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

Create an Azure resource

Start using the Bing Spell Check API by creating one of the Azure resources below.

  • Create a trial resource:

    • No Azure subscription needed.
    • Valid for seven days, for free. After signing up, a trial key and endpoint will be available on the Azure website.
  • Create a 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.
    • Bing Spell Check API is also offered in some tiers of the Bing Search v7 resource.
  • Create a Multi-Service resource:

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

Initialize the application

  1. Create a new Python file in your favorite IDE or editor, and add the following import statement.

    import requests
    import json
    
  2. Create variables for the text you want to spell check, your subscription key, and your Bing Spell Check endpoint. You can use the global endpoint below, or the custom subdomain endpoint displayed in the Azure portal for your resource.

    api_key = "<ENTER-KEY-HERE>"
    example_text = "Hollo, wrld" # the text to be spell-checked
    endpoint = "https://api.cognitive.microsoft.com/bing/v7.0/SpellCheck"
    

Create the parameters for the request

  1. Create a new dictionary with text as the key, and your text as the value.

    data = {'text': example_text}
    
  2. Add the parameters for your request. Append your market code after mkt=. The market code is the country you make the request from. Also, append your spell-check mode after &mode=. Mode is either proof (catches most spelling/grammar errors) or spell (catches most spelling but not as many grammar errors).

    params = {
        'mkt':'en-us',
        'mode':'proof'
        }
    
  3. Add a Content-Type header, and your subscription key to the Ocp-Apim-Subscription-Key header.

    headers = {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Ocp-Apim-Subscription-Key': api_key,
        }
    

Send the request and read the response

  1. Send the POST request using the requests library.

    response = requests.post(endpoint, headers=headers, params=params, data=data)
    
  2. Get the JSON response, and print it.

    json_response = response.json()
    print(json.dumps(json_response, indent=4))
    

Run the application

If you're using the command line, use the following command to run the application.

python <FILE_NAME>.py

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