This post intends to simplify consuming Representation State Transfer(REST) services in your Windows Store and Phone applications. Using REST Web Services frees you from having to store information yourself in your application or in your own service. There are a lot of services available for your applications to consume, including music, video, food, social networking, sports and weather. The following 6 steps can be used to add any public REST Service to your application, and for this specific post we will focus on retrieving restaurant data from the Yelp REST Service.
Step 1 – Find a service that provides the information you want
What kind of data are you looking to add to your application? The list below is categorized and provides a description of the service it provides. This is by all means not a complete list, but it does focus on mostly free API’s. Keep in mind that some RESTful API’s will cost money after a certain number of requests, so read the documentation for the services to find out the rules for using it before adding it to your application.
Fitness & Organized Sports
Step 2 – Sign up for a developer account and Register your application
You have found a service you want to add to your application. The next step is to sign up for a developer account for the service you selected and register your application. How this works will be different depending on the type of service you want to consume and is typically documented at the developer portal for the service. Sometimes developer account and application registration are done in the same step, and for other services you first register your developer account and then add applications you want to consume the service to your developer account. In either case you want to obtain the API Access ID to use in your application.
The example below is how you would sign up for a development account to consume the Yelp API’s.
- Go to the Yelp Developer Page
- Click Get Started
- Log into Yelp, or create an account. If you are creating an account, make sure to verify the new account before going on to next step.
- For this service it requests a website URL and Description on how you will use the API. I just specified my twitter URL, and a brief description.
- You will be taken to a page containing the information needed to access the Yelp API:
Step 3 – Save your Key and Secret in a safe place
You will have received a Key, and shared secret for your application to access the service. Save these in a safe place and do not share it with anyone. I typically define a global where I can access it when needed in my application:
Step 4 – Add references to your project for consuming the Service
- From the Visual Studio Tools menu choose Library Package Manager->Package Manager Console
- Make sure Package source is set to nuget.org and Default Project is set to your project
- At the prompt type the following command to install Newtonsoft JSON:
NOTE: You can also install NuGet Packages to your project by right clicking your project and choosing “Manage NuGet Packages…”. You can then search the online catalog for the package you want to install.
Next, we need to add a reference to make HTTP calls to the REST Service. Some REST Services, like Facebook have dedicated libraries for requesting/receiving information. If the REST Service you are consuming does not have a dedicated library then we can use System.Net.Http. This is available by default in Windows Store application projects. For Windows Phone projects follow the steps below.
- In the package manager console run the following command:
After adding the appropriate nuget packages to the application, we need to add references to them in the code.
Now the application is ready to make HTTP Requests to the REST Service.
Step 5 – Review the documentation for the service
All of the REST Services mentioned above will have detailed documentation on how to use the service. Some even have test sites used for testing out your requests. Make sure to follow all of the instructions for making requests to the service. Typically these involve sending a query string or JSON parameter with the client id and other information. Continuing with the Yelp Sample, lets get information, including ratings, for a specific restaurant based on its phone number. Documentation for request/response data for the Yelp Phone API Is located here:
Step 6 – Retrieve data from the service
Now that we have an understanding for how the Yelp API works it is time for the fun part. Typically you will want a class to wrap the API Calls since a lot of the information sent to the API will be similar. In our case the only thing that will be different is the phone number. Below is a sample class that will return data from the Yelp Phone API and write the data to the output window in the visual studio debugger.
When we run this code the following will be displayed in the Visual Studio Output Window:
That wraps up this post on how to add REST Application services to your Windows Store and Phone applications. Remember when writing your applications that you may not need to re-invent the wheel. Instead use your favorite search engine, like bing.com, to search for existing application services you can add to your application. I hope you can take the concepts illustrated in this article and apply it to the projects you are working on. Until next time, have fun coding!
Don’t forget to follow the Windows Store Developer Solutions team on Twitter @wsdevsol. Comments are welcome, both below and on twitter.
– Bret Bentzinger(Microsoft) @awehellyeah
JSON – http://json.org/
Representation State transfer(REST) – http://en.wikipedia.org/wiki/REST
NuGet – https://www.nuget.org/