Export (0) Print
Expand All
11 out of 20 rated this helpful - Rate this topic

Getting Started with Webmaster API

Quick start for Bing Webmaster API. Also check out the IWebmasterApi Members

Webmaster Api interface

Syntax

public interface IWebmasterApi

Here is where you can check out the IWebmasterApi Members

Remarks

Webmaster API support following protocols:
SOAP - Simple Object Access Protocol
POX/HTTP - Plain Old XML over HTTP
JSON/HTTP - JavaScript Object Notation over HTTP
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 Api 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" />

  7. 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);
            }
        }
    }
}

You will find samples for POX and JSON protocols on each of the method pages. 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
}

HTTP status code 400 will be returned in case of an exception for POX and JSON formats. The body will contain error description:

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.