Facebook Developers
DocumentationSupportBlogAppsLog In
  • Getting Started
  • Core Concepts
  • Advanced Topics
  • SDK Reference
    • JavaScript SDK
    • PHP SDK
    • iOS SDK
    • Android SDK
  • Tools
  • Core Methods
    • FB.api
    • FB.init
    • FB.ui
  • Auth Methods
    • FB.getAuthResponse
    • FB.getLoginStatus
    • FB.getSession
    • FB.login
    • FB.logout
  • Event Handling
    • FB.Event.subscribe
    • FB.Event.unsubscribe
  • XFBML Methods
    • FB.XFBML.parse
  • Canvas Methods
    • FB.Canvas.Prefetcher.addStaticResource
    • FB.Canvas.Prefetcher.setCollectionMode
    • FB.Canvas.getPageInfo
    • FB.Canvas.hideFlashElement
    • FB.Canvas.scrollTo
    • FB.Canvas.setAutoGrow
    • FB.Canvas.setDoneLoading
    • FB.Canvas.setSize
    • FB.Canvas.setUrlHandler
    • FB.Canvas.showFlashElement
    • FB.Canvas.startTimer
    • FB.Canvas.stopTimer

FB.api

SDK Reference › JavaScript SDK › FB.api

Overview

FB.api makes API calls to the Graph API or Deprecated REST API.

Examples

Get the Facebook Platform Page Object:

FB.api('/platform', function(response) {
  alert(response.company_overview);
});

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

FB.api('/me', function(response) {
  alert(response.name);
});

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

FB.api('/platform/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 story to their feed:

var body = 'Reading JS SDK 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');
  }
});

Best Practices

Making API calls directly to Facebook can improve the performance of your app, rather than proxying them through your own server.

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 connected status and the permissions that the user has granted your application.

Old REST API calls

FB.API can also be used to invoke calls to the Old REST API. For example, to call links.getStats:

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

Parameters

NameTypeDescription
pathString

the url path

methodString

the http method (default "GET")

paramsObject

the parameters for the query

cbFunction

the callback function to handle the response

Updated about 10 months ago
Facebook © 2012 · English (US)
AboutCareersPlatform PoliciesPrivacy Policy