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 🙏

© 2026 – Pkg Stats / Ryan Hefner

ts-messenger-api

v1.0.4

Published

The unofficial API for FB Messenger communication

Readme

ts-messenger-api (unmaintained!)

Unofficial API for Facebook Messenger. This package provides programmatic access to Facebook Messenger activity like sending and receiving messages on behalf of actual user.

Note: This package works only on NodeJS v15 and higher!

Since Facebook makes frequent modification to their API, please bear in mind, that this library is prone to developing bugs, that may not be immediately fixed.

Installation

You can install the package with NPM:

npm i ts-messenger-api

Quick Demo

import facebookLogin from 'ts-messenger-api';
// for plain JavaScript use this:
// const facebookLogin = require('ts-messenger-api').default;

const api = await facebookLogin({
  email: '[email protected]',
  password: 'your_messenger_password'
});

const friends = await api.getFriendsList();

await api.listen();
await api.sendMessage({ body: 'Hi' }, friends[0].id);

Message Sending

There are some examples of sending messages below. First of all, start with await api.listen(); to establish a websocket connection.

Plain text message

api.sendMessage({ body: 'This' }, friends[3].id);

Attachment message

Facebook automatically recognises file type (picture, video, audio or general file).

import fs from 'fs';
api.sendMessage({ attachment: fs.createReadStream('path-to-file') }, friends[3].id);
// or send multiple attachments in one message
api.sendMessage(
  {
    attachment: [
      fs.createReadStream('path-to-file1'),
      fs.createReadStream('path-to-file2'),
      fs.createReadStream('path-to-file3')
    ]
  },
  friends[3].id
);

Replying to a message

api.sendMessage(
  {
    body: 'This is my reply to your question',
    replyToMessage: originalMessageId
  },
  friends[3].id
);

Similarly, you can send a message with mentions. The types for that are in the docs.

Receiving messages and events

First of all, start with await api.listen() to establish a websocket connection. You can access the EventEmitter by using the returned value of this function or get it directly from api.listener property.

Posible event types are:

  • "message" for all incoming messages and events (for listening to events, specify the options argument as {listenEvents: true} while login)
  • "presence" for information about friends' active state
  • "typ" for incoming typing indicators
  • "error" for possible errors caused by websocket communication
api.listener.addEventListener('message', (msg: AnyIncomingMessage) => console.log(msg));
api.listener.addEventListener('error', err => console.error(err));

Type of an incoming message is defined in msg.type.

Login with AppState

You can login to Facebook account for the second time without the need to provide login credentials (email and password). This feature is provided with AppState (array of cookies provided by Facebook). We advise you to save the AppState right after first successful login using:

// after first login
fs.writeFileSync('./appState.json', JSON.stringify(api.getAppState()));

Now you can use the saved cookies to log in later.

// using the saved AppState
let api: Api | null = null;
try {
  api = (await login(
    {
      appState: JSON.parse(fs.readFileSync('./appState.json').toString())
    },
    { listenEvents: true }
  )) as Api;
} catch (error) {
  // something like `console.error(error);`
}

Docs

You can find the documentation here.

Features

The following table lists all features that are implemented or are destined to be implemented in the future. If you would like to have some feature implemented do not hesitate to submit PR.

| Feature | Implemented | | ---------------------- | :---------: | | Login | ✔ | | Logout | ✔ | | Get app state | ✔ | | Messages | | Send message | ✔ | | Unsend message | ✔ | | Delete message | ❌ | | Forward attachment | ✔ | | Set message reaction | ✔ | | Send typing indicator | ✔ | | Users | | Get friends list | ✔ | | Get user ID | ❌ | | Get user info | ✔ | | Threads | | Get thread list | ❌ | | Get thread info | ✔ | | Get thread history | ✔ | | Get thread pictures | ❌ | | Delete thread | ❌ | | Mute thread | ❌ | | Search for thread | ❌ | | Customisation | | Change thread colour | ✔ | | Change thread emoji | ✔ | | Group management | | Add user to group | ✔ | | Remove user from group | ✔ | | Leave group | ✔ | | Change admin status | ✔ | | Change group image | ✔ | | Change group title | ✔ | | Change nickname | ❌ | | Create poll | ❌ | | | | Mark as read | ✔ | | Mark as read all | ❌ | | | | Change archived status | ❌ | | Change blocked status | ❌ |