# Discourse2 on npm: a TypeScript wrapper for Discourse API

**URL:** https://meta.discourse.org/t/discourse2-on-npm-a-typescript-wrapper-for-discourse-api/328680
**Category:** Development
**Created:** [September 30, 2024, 12:56pm UTC](https://meta.discourse.org/t/discourse2-on-npm-a-typescript-wrapper-for-discourse-api/328680 "2024-09-30T12:56:19Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![gadicc](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gadicc/32/116696_2.png) [@gadicc](https://meta.discourse.org/u/gadicc)
#### Post date: [September 30, 2024, 12:56pm UTC](https://meta.discourse.org/t/discourse2-on-npm-a-typescript-wrapper-for-discourse-api/328680/1 "2024-09-30T12:56:19Z")

</div>

I originally published “[discourse2](https://www.npmjs.com/package/discourse2)” on npm a year ago, but recently gave it a lot more love. It’s basically a TypeScript / JavaScript SDK for the Discourse API, generated from Discourse’s OpenAPI spec. This makes it very easy to work with:

[![discourse completion](https://global.discourse-cdn.com/meta/original/4X/0/a/9/0a9f012b10a7ee0d9825ec0b929be2035effcd46.png)](https://github.com/gadicc/discourse2/blob/dev/assets/discourse-completion.png)

[![discourse getTopic type](https://global.discourse-cdn.com/meta/original/4X/e/f/3/ef3ad1384b1e0aa1c10e4004018d7b7486cb18d7.png)](https://github.com/gadicc/discourse2/blob/dev/assets/discourse-getTopic-type.png)

Usage looks like this:

```ts
import Discourse from "discourse2";

const discourse = new Discourse("https://discourse.example.org/", {
  "Api-Key": process.env.DISCOURSE_API_KEY,
  "Api-Username": process.env.DISCOURSE_API_USERNAME,
});

const result = await discourse.listLatestTopics();
console.log(result);

```

There is also a [live demo on CodeSandbox](https://codesandbox.io/p/sandbox/discourse2-dht4ym).

**Features** :

- The _entire_ Discourse API (that’s published in the OpenAPI spec).
- Always up-to-date: the OpenAPI spec is checked for changes daily, and the package will automatically rebuild and publish itself on changes. The most recent retrieval is shown as a badge at the top of the README: ![Discourse API update date](https://global.discourse-cdn.com/meta/original/4X/6/3/4/63499307e314e1fefd8c1f10314876f585a1fdba.svg)
- Works in both server and browser\* environments (\*useful for querying public data _without_ API keys and on relevant origin, e.g. latest topics, etc)

Please give it a star on [GitHub](https://github.com/gadicc/discourse2) so I can gauge interest and decide how much more time to spend on this. Feedback welcome. There are a few quirks as mentioned in the Notes section in the README.

---

<div class="post-metadata">

### Author: ![zueripat](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zueripat/32/454547_2.png) [@zueripat](https://meta.discourse.org/u/zueripat)
#### Post date: [October 9, 2024, 12:29pm UTC](https://meta.discourse.org/t/discourse2-on-npm-a-typescript-wrapper-for-discourse-api/328680/3 "2024-10-09T12:29:41Z")

</div>

Love the project, thanks a lot ❤

---

<div class="post-metadata">

### Author: ![gadicc](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gadicc/32/116696_2.png) [@gadicc](https://meta.discourse.org/u/gadicc)
#### Post date: [October 27, 2024, 10:19am UTC](https://meta.discourse.org/t/discourse2-on-npm-a-typescript-wrapper-for-discourse-api/328680/4 "2024-10-27T10:19:12Z")

</div>

Hi all, thanks for all the likes and support. A few updates:

✅ **86% test coverage** ; mostly E2E on a real Discourse instance. I think basically everything that works with `system` user, I need to experiment with other users for some methods.

✅ **Squashed a bunch of bugs** along the way, mostly related to PUT requests and redirects.

✅ **Published to JSR with 100% quality score**. All versions are still jointly published to NPM too.

✅ **Improved TSDocs**. Include `description` too when available, and not just `summary`. Also `@module` docs and `@example`’s.

✅ **Developed in Deno v2** for a fast, convenient develop experience. Still fully tested in `node` on every commit, but, alternate runtime friendly!

✅ **425 weekly downloads on NPM** (but presumably mostly due to bots and release frequency 😅).

I’m just about done with this for now. Still want to spend a bit more work on validation safety (and maybe some type coersion) but this is being used on real projects with good effect. Please open any issues for bugs or feature requests, and happy discoursing! 🎉
