Facebook Developers
DocsToolsSupportNewsApps
Log In
  • Social Plugins
  • Facebook Login
  • Open Graph
  • Facebook APIs
  • Games
  • Payments
  • App Center
  • Promote Your App
  • iOS
  • Android
  • JavaScript
    • Getting Started
    • How-Tos
    • Reference
  • PHP
  • More SDKs
  • Core Methods
    • FB.api
    • FB.init
    • FB.ui
  • Auth Methods
    • FB.getAuthResponse
    • FB.getLoginStatus
    • FB.login
    • FB.logout
  • Event Handling
    • FB.Event.subscribe
    • FB.Event.unsubscribe
  • XFBML
    • FB.XFBML.parse
  • Canvas Methods
    • FB.Canvas.Prefetcher.addStaticResource
    • FB.Canvas.Prefetcher.setCollectionMode
    • 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

JavaScript › Reference › FB.api

Overview

FB.api makes API calls to the Graph 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 to 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.

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 17 hours ago
Facebook © 2013 · English (US)
AboutAdvertisingCareersPlatform PoliciesPrivacy Policy