Hack 98 Search eBay for Listings that Accept PayPal

figs/expert.gif figs/hack98.gif

Use the eBay API to search for PayPal-enabled listings.

eBay and PayPal are a natural fit. eBay buyers love to pay with PayPal because it's quick and easy, so the vast majority of items listed for sale on eBay accept PayPal. This hack uses the eBay API to search for listings at http://www.ebay.com that accept PayPal.

Like PayPal API applications, eBay API applications can be written using any programming language and operating system. This hack uses the eBay Software Development Kit (SDK) for Windows and the C# programming language. eBay SDKs abstract away some of the implementation details of programming the API to make it easier to create an application. In addition to the SDK for .NET, eBay also provides an SDK for Java, as well as XML over HTTPS POST and SOAP interfaces. See eBay Hacks by David A. Karp (O'Reilly) for further coverage of the eBay API.

To create a search application with the eBay SDK, you must first perform a few preliminary setup steps:

  1. Sign up for the eBay Developers Program at http://developer.ebay.com. When you complete the registration process (which is free), you'll receive a set of developer keys you need to begin developing eBay applications against the eBay test environment, known as the Sandbox (different than the PayPal Sandbox [Hack #87] ).

  2. Download the eBay SDK for Windows and install it on your computer. Remember, even if you're not using Windows and .NET, you can still write applications using the eBay API. For instance, much of the API code in eBay Hacks is written in Perl, which can, of course, be used on virtually any platform and without needing to be supported by an SDK.

  3. Create a test user account on the eBay Sandbox. Go to http://sandbox.ebay.com, click Register at the top of the page, and fill out the form. Although the form looks just like the sign-up form used by eBay, an eBay Sandbox account is similar to a PayPal Sandbox account, in that it is merely a pseudo-account used just for testing your software application.

  4. Create a security token using the token generator located at http://developer.ebay.com/tokentool. This page takes the developer keys from step 1, as well as your sandbox user ID and password, and converts them into a security token that you can use for testing purposes. You pass the token to the eBay API server each time your application makes an API call.

8.14.1 The Code

Now that you've done the preparatory work, it's time to write your application. Create a Windows forms application with a small text box called txtSearch, a button called btnSearch, and a listbox, lstItem, in which to store the search output.

To call the functions in the SDK, begin by making a reference to the assembly eBay.SDK.dll from your project in Visual Studio .NET. Then, insert the appropriate include files at the top of your form's code window:

using eBay.SDK;

using eBay.SDK.API;

using eBay.SDK.Model;

using eBay.SDK.Model.Item;

Finally, create a Click event handler for the button that performs the search and displays the results:

private void btnSearch_Click(object sender, System.EventArgs e)


    IItemFoundCollection items;

    GetSearchResultsCall search = new GetSearchResultsCall(CreateSession( ));

    search.Query = txtSearch.Text;

    search.PayPalItemsOnly = true;

    search.MaxResults = 20;    // can be up to 200; more if you use paging

    items = search.GetSearchResults( );

    foreach(IItem it in items) 





Because the majority of the communication and data-handling code is wrapped by the classes provided by the SDK, the code you have to write is fairly straightforward. To do the search, this procedure simply creates an instance of the GetSearchResultsCall object, assigns values to its properties, and then calls the object's GetSearchResults method.

Setting the PayPalItemsOnly method to true filters out non-PayPal items.

The return value of GetSearchResults is a typed IItemFoundCollection that is populated with IItem objects, each of which represents an item listed for sale on eBay. After the function returns the collection of eBay items, the foreach loop uses it to populate the listbox with their titles.

There's one part of this code that can be a little tricky: creating a session object. The eBay ApiSession object is required to be passed to the server along with every eBay API call. Our event handler gets an ApiSession object by calling a function called CreateSession, which looks like this:

private ApiSession CreateSession( ) 


    ApiSession sess = new eBay.SDK.API.ApiSession( );

    sess.Developer = ConfigurationSettings.AppSettings["DeveloperID"];

    sess.Certificate = ConfigurationSettings.AppSettings["Certificate"];

    sess.Application = ConfigurationSettings.AppSettings["ApplicationID"];

    IApiToken t = new ApiToken( );

    t.Token = ConfigurationSettings.AppSettings["Token"];

    sess.Token = t;

    sess.Url = ConfigurationSettings.AppSettings["ServerUrl"];

    return sess;


8.14.2 Running the Hack

This code expects to find the configuration information it needs in a .NET XML configuration file, called Web.config if you're writing a web application or ExeName.config (where ExeName is the name of the executable) if you're creating a compiled binary application. A typical configuration file of an eBay application written in any .NET language looks like this:

 <?xml version="1.0" encoding="utf-8" ?>



     <add key="DeveloperID" value="mydevid" />

     <add key="ApplicationID" value="myappid" />

     <add key="Certificate" value="mycert" />

     <add key="ServerUrl" value="https://api.sandbox.ebay.com/ws/api.dll" />

     <add key="Token" value="AgAAAA**AQAAAA**aAAAAA**n8yAQA" />



For this to work, you need to replace the italicized values in the appSettings section (mydevid, myappid, and mycert) with your developer keys (sent to you from eBay after registering in step 1, earlier in this hack) and your security token (generated in step 4). Finally, the ServerUrl value provided here is the correct URL for the eBay development Sandbox. (You'll use a different URL to take the application live.)

Compile your application, and give it a whirl!

8.14.3 Hacking the Hack

There are many more things you can do with the eBay API besides search. One of the most common operations involves automatically listing items for sale, typically to save time in the selling process or provide integration between your inventory database and eBay. You can also use the eBay API to obtain details about listings in progress, download high-bidder information for completed items, and even create notifications when a bidder with negative feedback bids on one of your auctions! There are more than 70 calls in the eBay API, and the SDK provides quite a few code examples in a number of different programming languages.

?Jeffrey McManus