Consuming the Stack Exchange API with C#

Sign in to queue

Description

Need I say anything about the awesome that is Stack Overflow? Okay, maybe adding a walk-through of consuming its API with C# needs to be said...

Stack Exchange API

Two weeks ago I was playing around with the Stack Exchange API to see if it was easy to build a custom flair application. You know, this little guy:

image

How hard can it be to retrieve a user’s profile, badge count and reputation updates? Surely, Stack Exchange has an API. And they do…and it’s pretty easy to use. Let’s quickly cobble together a small sample application.

Table Of Contents

  • Introduction
  • API Usage
  • User Profile
  • Sending Requests
  • Reputation Changes

The post goes into some nice depth; from getting into the API;

API Usage

You can find the documentation for the Stack Exchange API here:

http://api.stackoverflow.com/

At the time of writing the latest release is v1.1, but a draft specification for version 2.0 has just been released.

What do you need to know about the API?

  • It’s read-only
  • All API responses are JSON
  • All responses are GZIP’d
  • API usage is throttled by IP address

To the tools/libraries you'll need;

So what do we need to be able to use the API?

  • It talks JSON? Fire up NuGet and get the popular Json.NET library.
  • We’ll need to issue requests. Let’s keep it simple and use the HttpWebRequest class
  • All responses are GZIP’d? Luckily the .NET framework provides us with the GZipStream class.

To writing code...

image

image

And in the end, some output;

image

BTW, to download the full sample, head over to his download page;

SNAGHTML2de19398

SNAGHTML2de09c9d

SNAGHTML2de5ef6e

So if you're interested in the Stack Overflow/Stack Exchange API, or just consuming a web service API that uses JSON, this project is waiting for you...

The Discussion

Add Your 2 Cents