The method FB.api() lets you make calls to the Graph API.
FB.api(path, method, params, callback)
Parameters
Name | Type | Description |
path | string | This is the Graph API endpoint path that you want to call. You can read the Graph API reference docs to see which endpoint you want to use. This is a required parameter. |
method | enum{get, post, delete} | This is the HTTP method that you want to use for the API request. Consult the Graph API reference docs to see which method you need to use. Default is get |
params | object | This is an object consisting of any parameters that you want to pass into your Graph API call. The parameters that can be used vary depending on the endpoint being called, so check the Graph API reference docs for full lists of available parameters. One parameter of note is access_token which you can use to make an API call with a Page access token. App access tokens should never be used in this SDK as it is client-side, and your app secret would be exposed. |
callback | function | This is the function that is triggered whenever the API returns a response. The response object available to this function contains the API result. |
Example: Read the JavaScript Facebook Page:
FB.api(‘/113124472034820’, function(response) {
console.log(response);
});
Example: Return the last name of the current user:
FB.api(‘/me’, {fields: ‘last_name’}, function(response) {
console.log(response);
});
Example: Publish a status message to the current user’s 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);
}
});
Example: 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’);
}
});
Example: Reading a Page’s messages using a Page Access Token:
var pageAccessToken = ‘1234567890|faketoken’;
FB.api(‘/me/conversations’, {
access_token : pageAccessToken
});