node-discourse-api

node-discourse-api

Unofficial, Discourse Api wrapper for Node

This package is a work in progress and a large number of features are still under development. If you need to add functionality, please pull request to add a new method, or use api._request to request any API endpoint.

⚠️IMPORTANT: Regarding version numbers, any version before releasing version 1.0.0 SHOULD NOT be considered a stable version and we MAY change the API and cause incompatibilities. When there is an incompatible API, we will increment the minor version. When a new API is covered, we will increment the patch version.

build status doc status lint status

Home page

API documentation

Development Progress

❌: Not started yet
🟩: Work in progress
βœ…: Completed

API Progress
Backups ❌
Badges ❌
Categories ❌
Groups ❌
Invites ❌
Notifications βœ…
Posts βœ…
Topics 🟩
Private Messages ❌
Search ❌
Site βœ…
Tags ❌
Uploads 🟩
Users 🟩
Admin ❌
Chat 🟩
Webhook 🟩

How to use

Installation

npm install node-discourse-api

Example

Start

const { DiscourseApi } = require("node-discourse-api");

const api = new DiscourseApi("https://discourse.example.com");

// API configured by Discourse administrator. You can also leave it unset and have the API read only the public content of your forum.
api.options.api_username = "API_USERNAME";
api.options.api_key = "API_KEY";

Create a topic, Post a reply

// Create a new topic, then print it to console
api
.createTopicPostPM({
raw: "This is a post from node-discourse-api!!!",
title: "Hello world!!!",
})
.then((post) => {
console.log(post);
});
// Post a reply
api.createTopicPostPM({
raw: "This is a reply post from node-discourse-api!!!",
topic_id: 1,
});
// Reply to a post
api.createTopicPostPM({
raw: "This is a reply of reply from node-discourse-api!!!",
topic_id: 1,
reply_to_post_number: 2,
});
// Handling errors
api
.createTopicPostPM({
raw: "This reply cannot be sent",
topic_id: -123,
})
.catch((err) => {
console.log("Catched a error");
console.error(err);
});

Send a message in channel

// Send a message in channel 2. Probably, it's #general
api.chat.sendMessage(2, "hello, world!");

Generate user api key

const { DiscourseApi } = require("node-discourse-api");

const api = new DiscourseApi("https://discourse.example.com");

const generated = api.generateUserApiKeySync();

console.log(
"Please visit the following URL authorization and paste the generated key in the console:",
);
console.log(generated.url);

const readline = require("readline").createInterface({
input: process.stdin,
output: process.stdout,
});

readline.question(`Paste key: `, (key) => {
const decrypted = api.decryptUserApiKey(generated.private_key, key);
console.log(decrypted);

api.options.user_api_key = decrypted.key;

api.createTopicPostPM({
raw: "This is a post from node-discourse-api!!!",
title: "Hello world!!!",
});

readline.close();
});

For more examples, see the API documentation

Contribute to this project

Initialize

We have set up some git hooks to help maintain code quality. If you would like to contribute to this project, please initialize in this order

git clone https://github.com/Lhcfl/node-discourse-api
cd node-discourse-api
yarn install
yarn devprepare

Test and autofix

Test code:

yarn test

autofix:

yarn autofix

Generated using TypeDoc