Using the Facebook Query Language (FQL)

Lots of people often want to use the Facebook API for many reasons – get some user data, social plugin, insight access, etc. The most widely used method seems to be the FB graph API. Although the Graph API is quite simple to use, it gets a bit clumsy if one needs a complex request or even to filter out some parts of data. Let us say, for example, we need to gather the feeds from our news feed. We could simply use the graph API to make a call to{used_id}/home and conveniently receive something like this:

There may be two things problematic with this response.

  1. This may be much more data than we actually need, in which case we are unnecessarily sending bigger requests to the server every time we need new information.
  2. Notice that the above response has a post_id and an actor_id for each feed. What happens if we need the name of the poster (actor_name)? We would have to make a request to the graph API for every single feed item. That’s a lot of requests!

A much better solution would be the Facebook Query Language (FQL), which is basically a subset of SQL. It comes with the following major advantages over the Graph API:

  1. It allows you to query against specific tables. For instance, the user table for users, the stream table for any sort of feeds, so on and so forth.
  2. It allows multi-query in a single request. Although it still does not give the power of JOIN like SQL does, it will let you write sub-queries and use almost all of the major SQL operations which can be extremely helpful when querying multiple tables. And all this, in a single request!

Lets get started then! We will build a simple MVC3 application that will access the Facebook via the FQL using multiple queries, get the newsfeed of a certain user (in this case, mine), and then parse it out into a view. To keep things simple, I have not included anything regarding obtaining access_tokens via client-side or server-side. If you are interested in that, the FB documentation does a pretty good job here:

Before we begin, you will need:

  • Log in to
  • Create a test application – Just fill out the basic information for now and you should be good.
  • Go to the graph explorer:
  • Click in Get Access Token to obtain an access token. Make sure you check all options and all three permission tabs in the dialog that pops up so that your app will have full access to your Facebook. For the news feed however, the minimum amount of access required is all user_{action} and read_stream. Once, done, copy the access code and save it for later use. Note: This access_token will expire within one or two hours, so if you get an OAuth Exception later, you will have to retrieve a new access token following these steps again. The permissions dialog looks like this:

So now that you have your access_token, we are ready to begin. Let us first start a new MVC3 project and then we will proceed to build out each of the three MVC parts.


Since the news feed is comprised for the entire stream, which will consist of individual feeds which will further contain properties and perhaps media as well, the following model should form a pretty simple, but a representative model of our Facebook feed.


Our controller needs to do the following this:

  1. Connect to the Facebook API with our access_token
  2. Parse the response into an acceptable data structures and map them to our model
  3. Do any required conversions along the way (dates and times etc.)

To achieve that, we will need a few extra tools. You may choose the library of your choice, but I have chosen the Facebook C# SDK and NewtonSoft’s Json Parser to achieve our controller goals. To install this simply use the Package Manager Shell:

  1. PM> Install-Package Facebook
  2. PM> Install-Package Newtonsoft.Json

Once you have those references, the following code should be the basic boilerplate to get the news feed.



I have added a tiny bit of exception handling into the view, so that it was easier to track what was going. If you are planning on publishing, perhaps using a custom error page is better than showing the users the actual stack trace 🙂 Well, here is the view.

And that’s that! This should give give you a good headstart to the Facebook API with FQL! Good luck! Here is what the output looks like.

Blog Comments

I think this was a very interesting post thanks for writing it!

Thank you. I am glad you enjoyed it.

Thanks for another great write-up. The site otherwise might anyone obtain that form of facts in such an excellent way involving publishing? I get a business presentation in a few days, that i’m in the find similarly info.

Leave a Comment