Skip to content
Research notes on data, code, and analysis +1 2354253223 [email protected] Los Angeles, California

Using Node.js to Interact with Facebook's Graph API

A colleague presenting a technical finding

Facebook has a JavaScript SDK for interacting with their Graph API, but it is for client side JavaScript. There is no Node.js SDK. Although there are several NPM packages for working with Facebook's Graph API, no NPM package seems to be as mature as packages for other social media platforms like Twit for the Twitter API. The best of the few seems to be the Facebook Node SDK, but I hesitate to rely on it for a business critical app. The Graph API docs have no examples using vanilla JavaScript, although there are a few examples using their client side SDK.

This post is filled with useful examples using Node and Express. Contents: authorization and access tokens, querying the search route, querying a single graph object, querying for the pages a user manages, and posting content.

1. Authorization and access tokens

To get started, you need an access token obtained through Facebook authentication. If you have an access token at your disposal, you can skip the rest of this section. If you need to get one, read on.

Generally, an app acquires a user access token through an authentication procedure. Passport.js is a great authentication package for Node.js apps. The Passport.js Facebook authentication strategy comes with a very useful demo app that uses Express.js. If you need to acquaint yourself with Passport, that repository is a good place to start.

Getting a token

Unfortunately, it is not as easy as merely firing up the example and authenticating. You must have an app registered with Facebook. Instructions for doing so can be found in the Facebook Graph API documentation.

Once you have a registered app, you will need to use the app id and app secret in the config file of your authentication app. Adjust the code in the demo app so that you log the returned access token, as you will need the token for the requests outlined below.

Getting POST permissions

Not all access tokens are created equal. If you intend to post to a user's feed or to a page managed by a user, you need to specifically ask for publish actions and manage pages permissions through a scope object in your authentication.

When you ask for these permissions, a user logging in will see a prompt stating that your app is requesting these permissions. If the user clicks the okay button, an access token with the specified permissions is generated.

The app review process

Without submitting your app to an official Facebook review process, the above code will only give you public profile access. To request additional permissions you have to prove that you are a well intentioned developer with a well intentioned app. For the right to obtain access tokens with additional permissions, you will have to fill out the App Review section of your app's settings page in the Facebook Developers website. The review process is quite an endeavor, and requires you to describe your app and submit a video walk through of how your app is used.

If you have an access token, you are ready to get started. Although the Node and Express route is a POST route, the request we are sending to the Graph API is a GET. For the app we are building, the value assigned to the search type could be page or user, and each type will return different properties. In reality, the search type could be any of the objects listed in the documentation.

Notice the fields property and the user field set and page field set constants. When searching pages, the code requests the name, category, link, picture, and verified fields to be returned. These are all part of the page public profile and do not require special permissions. The fields requested when searching users are part of the user public profile. Although these fields are all publicly available, this data will not be returned unless specified in the fields property of the request.

The Graph API's search endpoint only returns publicly available information, and there is a good chance that you will want more than this. You will probably want the additional information that your user access token permits you to read.

3. Querying a single graph object

This section covers what you will need to do to get information about a single object. It starts with querying a single user, and ends with querying a single photo.

When querying the search route, an id is returned regardless of the object type and fields specified. You need this id to make a single object request, for example a page, user, event, photo, or post.

Permissions: if you have a Graph object's id, you can request detailed data as long as the access token you are using has permissions for the fields you are requesting.

Although a lot of information is returned, much important information is left out. For example, no URLs are provided for the photos in my photo library. To get a photo's URL, you have to ask for the link field of the photo. Photos is a field of a user node, and link is a field of a photo. To get the link of each photo in our response, we need to request a field of a field.

The syntax for requesting the field of a field is the outer field with a nested field in braces. To request more than one nested field, separate them with commas. While we are on the topic of useful syntax to work with the Graph API, there is also a way to limit the number of items returned.

4. Querying for the pages a user manages

This request is probably not one you need to use very frequently, but if your app needs to manage the pages for which the user has admin permissions, this example will be useful.

In the example below, notice that the fields property is not included because the data you are requesting is implicit in the URL. The user query in section three above also returned this information because the user access token that is used has permissions for this information, and accounts is specified in the fields property of the request.

If you have accounts permissions, and only want a list of accounts the user manages, rather than all user data, then this is the query you want to perform.

References and further reading

Primary sources for the platform and packages in this note.

  1. Facebook Graph API for platform endpoints.
  2. Facebook JavaScript SDK for client side examples.
  3. facebook node sdk for Node.js access.
  4. Passport.js for authentication strategies.
  5. Passport Facebook strategy for the login flow.
  6. Graph API Explorer for testing queries.
  7. Facebook app review for permissions.
  8. OAuth 2.0 for the authorization model.
  9. Express for the server layer.
  10. MDN Web Docs for HTTP request methods.
  11. MDN JSON reference for reading responses.