Using Webmaster API

Guide on using Bing Webmaster APIs along with steps on using SOAP protocol with some sample calls and error responses.

Webmaster Api interface

Syntax

public interface IWebmasterApi

Check out the IWebmasterApi Members

Remarks

SOAP protocol

SOAP protocol url is https://ssl.bing.com/webmaster/api.svc/soap?apikey=API_KEY. WSDL can be found on https://ssl.bing.com/webmaster/api.svc?wsdl.

To start work with Webmaster APIs via SOAP protocol you need to perform following steps:

  1. Create a new C# Console project in Visual Studio.

  2. Right click on the project in Solution Explorer and select the "Add Service Reference" context menu option.

  3. Enter https://ssl.bing.com/webmaster/api.svc in the "Address" edit box.

  4. Enter WebmasterApi in the "Namespace" edit box.

  5. Click "Ok". Visual Studio will generate service reference files and will add service configuration section to app.config.

  6. Open app.config and find the system.serviceModel/client/endpoint section. You have to replace the API_KEY string in the address attribute with your own Webmaster API Key (you have to generate it on the Bing Webmaster Tools site). You will get something like this:

<endpoint address="https://ssl.bing.com/webmaster/api.svc/soap?apikey=EEDECC1EA4AE341CC57365E075EBC8B6" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IWebmasterApi" contract="WebmasterApi.IWebmasterApi" name="BasicHttpBinding_IWebmasterApi" />
  1. You also need to increase maximum size for incoming messages. Find system.serviceModel/bindings/binding section. Change value of maxBufferSize and maxReceivedMessageSize attributes to 524288 or larger number.

Then you will be able to perform requests to Webmaster API. The following example demonstrates how to print traffic information for the last month to the console (Replace http://yoursite.com/url with the URL of your site which has been verified in Bing Webmaster Tools).

namespace WebmasterApiExamples
{
    using System;
    using System.Linq;
    using System.ServiceModel;

    internal class Program
    {
        private static void Main(string[] args)
        {
            var api = new WebmasterApi.WebmasterApiClient();

            try
            {
                var oneMonthAgo = DateTime.Now.AddMonths(-1);
                var stats = api.GetRankAndTrafficStats("http://yoursite.com/")
                    .Where(s => s.Date > oneMonthAgo)
                    .OrderBy(s => s.Date);
                Console.WriteLine("Date\tImpressions\tClicks");
                foreach (var value in stats)
                {
                    Console.WriteLine("{0}\t{1}\t{2}", value.Date.ToShortDateString(), value.Impressions, value.Clicks);
                }
            }
            catch (FaultException<WebmasterApi.ApiFault> fault)
            {
                Console.WriteLine("Failed to add site: {0}", fault.Message);
            }
        }
    }
}

In case of errors ApiFault will be generated. A SOAP protocol error can be caught using the following code:

catch (FaultException<WebmasterApi.ApiFault> fault)
{
    // handle error
}

The samples for POX and JSON protocols can be found on each of the method pages. In case of an exception, HTTP status code 400 will be returned for POX and JSON formats. The body will contain error description as shown in the samples below:

POX error response sample

HTTP/1.1 400 Error
Content-Length: 97
Content-Type: application/xml; charset=utf-8

<root type="object"><ErrorCode type="number">3</ErrorCode><Message>InvalidApiKey</Message></root>

JSON error response sample

HTTP/1.1 400 Error
Content-Length: 41
Content-Type: application/json; charset=utf-8

{"ErrorCode":3,"Message":"InvalidApiKey"}

See Also

Concepts

IWebmasterApi Members