npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

facebookgraph

v0.4.0

Published

async/await Node.js package to access Facebook Graph API

Downloads

77

Readme

facebookgraph

facebookgraph: Node.js client for the Facebook Graph API with Flow support

facebookgraph is a Node.js client/interface for the Facebook Graph API.

  • it uses axios instead of requests
  • it uses async/await syntax
  • it works only with Node.js 7.6+
  • it uses TypeScript
  • it comes with built-in pagination for fetching data from particular nodes or via search

Table of Contents

Install

yarn add facebookgraph

or

npm install facebookgraph

Usage

In order to use Facebook Graph API you need to have an access token which is being used to initialize FacebookGraph object. Here's an example how to fetch 5 posts of the page with the id 523008607856853.

const FacebookGraph = require('facebookgraph');

const graph = new FacebookGraph('<Your Facebook Access Token>')
const posts = await graph.fetch('523008607856853', 'posts', 5)
console.log(posts);

Request object using ID

const zuck = await graph.get('4');
{ id: '4',
  first_name: 'Mark',
  last_name: 'Zuckerberg',
  link: 'https://www.facebook.com/app_scoped_user_id/4/',
  name: 'Mark Zuckerberg',
  updated_time: '2017-01-26T08:32:59+0000' }

Search API

Search API endpoint is: https://graph.facebook.com/v2.8/search. You can search through objects of type user, page, event, group, place, placetopic.

The payload object has three properties: a search term (q), a search type (type) property and fields of each object. Check out the docs to see which fields can be requested for each object.

const pages = await graph.search({ q: 'geek', type: 'page', fields: 'name, link' })

The code above requests the name and link fields to be returned which are the part of the page public profile and do not require additional permissions. The Facebook Graph API's /search end point only returns publicly available information.

The syntax for requesting the field of a field is field{nestedField}; to request more than one nested field, separate them by commas: field{nestedField1, nestedField2, nestedField3}; e.g. specify photos.limit(2) to return only 2 photos, or photos.limit(2){link, comments.limit(3)} to return only 2 photos but link and up to 3 comments for each one.

const users = await graph.search({ q: 'geek', type: 'user', fields: 'photos.limit(2){link, comments.limit(2)}' }

Pagination

By default, .search and .fetch get only first 25 corresponding elements. It is possible, however, get more results as pagination is already incorporated. There is a 2nd paramater size for both .search and .fetch which defines how many elements should be fetched in total: it can be set either to a particular number or as Infinity to go through all the results pages and gather all results

const pages = await graph.search({ q: 'geek', type: 'page', fields: 'name, link' }, Infinity)
const posts = await graph.fetch('523008607856853', 'posts', 100)

Posting text messages, photos or videos

const post = await graph.post('me', { message: 'This is a test message.', link: 'https://zaiste.net' });

Set no_story to hide the post from showing up in the user feed.

Batch requests

const r = await graph.batch([
  { method: "GET", relative_url: "me"},
  { method: "GET", relative_url: "me/friends?limit=10"}
])