From Facebook Developer Wiki (FbDevWiki.com)
Jump to: navigation, search

Make a API call to the Graph API or REST API.

Server-side calls are available via the JavaScript SDK that allow you to build rich applications that can make API calls against the Facebook servers directly from the user's browser. This can improve performance in many scenarios, as compared to making all calls from your server. It can also help reduce, or eliminate the need to proxy the requests thru your own servers, freeing them to do other things.

The range of APIs available covers virtually all facets of Facebook. Public data such as names and profile pictures are available if you know the id of the user or object. Various parts of the API are available depending on the connect status and the permissions the user has granted your application.

Except the path, all arguments to this function are optional.

Get the f8 Page Object:

FB.api('/f8', function(response) {

If you have an authenticated user, get their User Object:

FB.api('/me', function(response) {

Get the 3 most recent Post Objects Connected to (in other words, authored by) the f8 Page Object:

FB.api('/f8/posts', { limit: 3 }, function(response) {
  for (var i=0, l=response.length; i<l; i++) {
    var post = response[i];
    if (post.message) {
      alert('Message: ' + post.message);
    } else if (post.attachment && post.attachment.name) {
      alert('Attachment: ' + post.attachment.name);

If you have an authenticated user with the publish_stream permission, and want to publish a new story to their feed:

var body = 'Reading Connect JS documentation';
FB.api('/me/feed', 'post', { message: body }, function(response) {
  if (!response || response.error) {
    alert('Error occured');
  } else {
    alert('Post ID: ' + response.id);

Or if you want a delete a previously published post:

var postId = '1234567890';
FB.api(postId, 'delete', function(response) {
  if (!response || response.error) {
    alert('Error occured');
  } else {
    alert('Post was deleted');

Old REST API calls

This method can also be used to invoke calls to the Old REST API. The function signature for invoking REST API calls is:

FB.api(params, callback)

For example, to invoke links.getStats:

    method: 'links.getStats',
    urls: 'facebook.com,developers.facebook.com'
  function(response) {
      'Total: ' + (response[0].total_count + response[1].total_count));


Name Type Description
path String the url path
method String the http method (default "GET")
params Object the parameters for the query
cb Function the callback function to handle the response
Personal tools
Graph API