Introduction to the Facebook Graph API
Get introduced to Facebook Graph API.
We'll cover the following
Importance of the Facebook Graph API
Facebook’s Graph API is the central means for programmatic access to Facebook data. It is the entry point for reading and writing to Facebook’s “social graph” — that is, a user’s posts, pages, photos, friends, etc.
Assuming you have the authorization to do so, the Facebook Graph API lets you access everything from a user’s Facebook Messages to a user’s Live Videos to a business’s Facebook Ad Campaign.
Multiple individual APIs under one umbrella
Because Facebook’s Graph API is so massive, it breaks different sections of its data into individual APIs. This makes it easier for you, the developer, to find your way around and not feel overwhelmed. But keep this in mind: it is all part of the Facebook Graph API. With the exception of a few sections, all of it is accessible through the same host URL: graph.facebook.com
.
Facebook’s central documentation page is a good starting point for finding details about the functionality of its APIs. Below are some examples of commonly used sections, all of which are part of the Facebook Graph API:
- Facebook Messenger platform — for managing messaging between a user and a business with a Facebook page
- Marketing API — for managing advertising on Facebook
- Instagram Graph API — for managing accounts on behalf of Instagram professionals (businesses and creators)
- Groups API — for reading and creating data on behalf of Facebook Group members
This is just the tip of the iceberg! As you can see, knowing your way around the Facebook Graph API will open unlimited possibilities for rich application development.
Organized around the “social graph”
The Facebook Graph API is organized around a “social graph”, which is a different way to think about data and relationships than the traditional relational data model. If you come from a background where you learned about data models alongside relational database systems like MySQL, the graph theory model of structuring data may be new to you.
The Facebook social graph is a model for how data in a social network ought to be structured and presented. It emphasizes relationships between data objects, and even those relationships hold data in themselves.
A departure from relational databases and hierarchical associations
Let’s first think about the traditional way of approaching data models with a relational database. Relational databases organize their data in tables. Data objects have attributes, and they are related to other data objects with some sort of has_one
or has_many
or belongs_to
type relationships. In relational databases, these associations are facilitated by foreign keys and join queries.
To give an example, you might have an entity like a Movie
. A Movie
has its own attributes like title
, genre
, and release_year
.
Movies are often filmed in many locations, of course, and so there is another entity called Location
. A Location
has a country
and city
. A Movie
can have many Locations
, and a Location
might be associated with many Movies
.
Movies also have actors, and so we have the Actor
entity. Each Actor
has a screen_name
, legal_name
, and date_of_birth
. A Movie
can have many Actors
, and an Actor
can be in many Movies
.
You see where this is going.
To round out our example, let’s just note that a Movie
might be a part of a Series
. So, there might be a Series
that has many Movies
, but there might be some Movies
that aren’t associated with any Series
. Also, there might be Awards
to which a Movie
or an Actor
might be associated, either for being nominated or for being the winner.
We end up with a data model that looks something like this:
In our example, common queries might include something like, “Get the names of all the movies that the actor, Tom Hanks, has starred in.” Or perhaps, “Get me the names of any actor who has been nominated for the Best Actress Oscar award.”
These are reasonable, simple queries, and retrieving this information in a relational database would use basic table join queries in SQL.
When everything has a relationship with everything
Imagine instead if the query we needed was: “What is Alan Rickman’s Bacon Number?” That is, how many degrees of separation (movies to co-star actors to those actors’ movies, etc.) are there between Alan Rickman and Kevin Bacon? We would need to consider all of the movies that Alan Rickman has been in. For each of those movies, find all of the Actors
in those Movies
. For each of those Actors
, find all other Movies
that they have been in, and keep on crawling in the database until we find Kevin Bacon.
As a data model grows in complexity, with more and more data types — and increasingly complex associations between data types — the relational database approach of your data models can feel unwieldy. Table joins can get out of hand, not to mention them becoming very computationally expensive.
Real-life sometimes just doesn’t fit in neat little tables, rows, and columns. Social relationships are no exception.
A better solution comes to us through graph theory.
Facebook’s Graph API gets its name from its “social graph”, which is related to graph theory. Everything about graph theory and Facebook’s social graph revolves around the modeling of relationships between objects. Graph theory is often the go-to tool for modeling social relationships (i.e., who is friends with whom).
Modeled as a graph, our Movies
and Actors
relationships would look more like this:
Nodes, edges, and fields
In the Facebook Graph API, we have nodes, edges, and fields.
Nodes are individual objects — for example, a user, a post, a video, a photo, or a poll. Every object has a unique object id.
Edges are connections between a collection of objects and a single object — for example, the friends of a user, or the photos on a page, or the likes on a post.
Fields are data about an object itself, like a user’s email address, or a post’s text content.
As we use the Facebook Graph API, we will be thinking about our data and queries in these terms: nodes, edges, and fields.
Multiple ways to access the Graph API
Facebook has developed a sizable set of official SDKs that make interacting with its Graph API simple and convenient. These include JavaScript, Android, iOS, React, Swift, etc. Ultimately, though, using something even as basic as cURL
at the command line is sufficient for making requests of the API.
Regardless of the language or framework used to work with the Facebook Graph API, every developer needs to become familiar with the foundational concepts of authentication, authorization, and query structure. We will talk about this in our next lesson.
Test yourself
The Facebook Graph API is modeled around the traditional relational database approach to modeling data with tables, rows, and columns.
True
False