Back to News for Developers

Batch requests in Graph API

March 17, 2011ByTR Vishwanath

The Graph API is designed to make it easy to get data for an individual object and to browse connections between objects. If your app needs the ability to access significant amounts of data or make changes to several objects at once, it is more efficient to combine these operations than to make multiple HTTP requests. Starting today, you can batch requests with the Graph API.

Batching allows you to pass instructions for several operations in a single HTTP request. You can also specify dependencies between those operations. Facebook will process each of your independent operations in parallel, and once all operations have been completed, a consolidated HTTP response will be passed back to you. We have enabled simple batched requests as well as the ability to batch multiple HTTP methods and FQL queries.

To make a simple batched request, build a JSON object which describes each individual operation to be performed and POST it to the Graph API endpoint at The following example gets the current user's profile information and the first 50 friends from their friends list in a single request in PHP:


  $app_id = "YOUR_APP_ID";
  $app_secret = "YOUR_APP_SECRET"; 
  $my_url = "YOUR_URL";

  $code = $_REQUEST["code"];

  if(empty($code)) {
    $dialog_url = "" 
      . $app_id . "&redirect_uri=" . urlencode($my_url);

    echo("<script> top.location.href='" . $dialog_url 
      . "'</script>");

  $token_url = ""
    . $app_id . "&redirect_uri=" . urlencode($my_url) 
    . "&client_secret=" . $app_secret 
    . "&code=" . $code;

  $access_token = file_get_contents($token_url);
  $batched_request = '[{"method":"GET","relative_url":"me"},' .

  $post_url = "" . "?batch=" 
    . $batched_request
    . "&access_token=" . $access_token . "&method=post";
  echo $post_url;

  $post = file_get_contents($post_url);
  echo '<p>Response: <pre>' . $post . '</pre></p>';


Once both operations have been completed, Facebook sends a response which encapsulates the result of all operations. For each operation, the response includes a status code, header information, and body. This is equivalent to the response you expect from each operation if performed as raw requests against the Graph API. The body field contains a string encoded JSON object:

For the above example, the expected response would be of the form:

    { “code”: 200, 
          { “name”: ”Content-Type”, 
            “value”: ”text/javascript; charset=UTF-8” },
       { “name”:”ETag”, 
         “value”: ”…”
      “body”: ”{\”id\”:\”…\”}”},
    { “code”: 200,
          { “name”:”Content-Type”, 
            “value”:”text/javascript; charset=UTF-8”},
          { “name”:”ETag”, 
            “value”: ”…”
      “body”:”{\”data\”: [{…}]}}

Batching also allows you to implement more advanced features like combining operations that would normally use different HTTP methods into a single batch request, using FQL, and ordering operations to express dependencies.

More can be found in our documentation on the Developer Site. We hope that batching helps you build more robust and efficient apps.

Please let us know if you have any thoughts or questions in the comments below.