Shell Script That Automates URL Submission to Bing

- 2 minutes read

Microsoft’s search engine, Bing, offers a free API for webmasters.

This API enables webmasters, such as myself, to submit our new URLs for indexing.

What’s really cool is that your fresh new pages can show up in search results within just ten minutes or so, sometimes even quicker.

While Bing allows you to manually submit URLs via their Webmaster Tools dashboard, I didn’t want to have to sign in each time.

So I made a simple shell script to automatically submit my URLs with cURL:

#!/bin/bash

var() {
    VAR=$(grep $1 $2 | xargs)
    IFS="=" read -ra VAR <<< "$VAR"
    echo ${VAR[1]}
}

BING_API_KEY=$(var BING_API_KEY .env)

site="https://example.com"

url=$1

curl -X POST "https://ssl.bing.com/webmaster/api.svc/json/SubmitUrlBatch?apikey=$BING_API_KEY" -H "Content-Type: application/json" -H "charset: utf-8" -d '{"siteUrl":"'$site'", "urlList":["'$url'"]}'

Be sure to replace site with the base URL of your site, in double quotes!

Let’s save it to a file called bing.sh.

And in the same directory, let’s add a file called .env.

In the .env file, add the following line, replacing your Bing Webmaster API key with EXAMPLE:

BING_API_KEY=EXAMPLE

Link to this section Usage

Then just run the following command from your terminal:

./bing.sh <url>

Here is an example (I’m going to run this as soon as I post this article!):

./bing.sh https://natclark.com/tutorials/shell-automate-bing-submission/

Lastly, as of writing this, a successful response will print this output:

{"d":null}

Otherwise, cURL will display an error message.

Link to this section Conclusion

The full script is also available as a Gist on my GitHub account.

Enjoy your speedy display in search results!