Skip to content

Getting Started

m4bwav edited this page Sep 29, 2026 · 1 revision

Install the package, then call retrieveMarkdown with a post id. Every route below was run on 2026-09-29 on Windows 11 against the published 2.0.0; each one printed the sample post that the local stand-in for the API served (see Home).

Install

Tool Command Tested with
npm npm install stack-exchange-markdown-retriever npm 11.16.0, Node 24.18.0
pnpm pnpm add stack-exchange-markdown-retriever pnpm 10.34.5 (through corepack)
Yarn yarn add stack-exchange-markdown-retriever Yarn 4.18.1 (through corepack), Plug'n'Play
Bun bun add stack-exchange-markdown-retriever Bun 1.4.2
Deno import ... from 'npm:stack-exchange-markdown-retriever@2.0.0' Deno 2.9.6

The package needs Node 20 or later (engines is >=20) and has no runtime dependencies. The whole library was also run on Node 20.20.2; where Node 20 answers differently, the page says so.

ES modules

import {retrieveMarkdown} from 'stack-exchange-markdown-retriever';

const markdown = await retrieveMarkdown({site: 'scifi', entityId: 127968});
console.log(markdown);

That prints (sample content; the real post is https://scifi.stackexchange.com/q/127968):

Sample question body, made up for this wiki (not the real post).

It keeps HTML entities: "quoted" & more.

    an indented code line

The same file printed the same text under pnpm (node example.mjs), Bun (bun example.mjs) and Yarn 4 (yarn node example.mjs).

The default export holds both named exports:

import retriever from 'stack-exchange-markdown-retriever';

const markdown = await retriever.retrieveMarkdown({entityId: 11227809});
console.log(markdown);

It prints (sample content; compare the real post at https://stackoverflow.com/q/11227809):

Sample Stack Overflow question, made up for this wiki.

CommonJS, with a callback

const stackExchangeMarkdownRetriever = require('stack-exchange-markdown-retriever');

stackExchangeMarkdownRetriever.retrieveMarkdown({site: 'scifi.stackexchange.com', entityId: 127968}, (markdown, error) => {
  if (error) {
    console.error(error.message);
    return;
  }

  console.log(markdown);
});

It prints the same sample text as the ES module example. The callback gets (markdown, error), markdown first, which is 1.x's order and not Node's usual error-first order. Without a callback, require users get the same Promise as ES module users.

Yarn 4 needs yarn node

Yarn 4 installs with Plug'n'Play and writes no node_modules folder, so plain node example.mjs fails:

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'stack-exchange-markdown-retriever' imported from <project>/example.mjs
exit 1

(<project> stands for the project's folder.)

Run scripts with yarn node example.mjs instead, or set nodeLinker: node-modules in .yarnrc.yml (not tested here).

Deno

import {retrieveMarkdown} from 'npm:stack-exchange-markdown-retriever@2.0.0';

const markdown = await retrieveMarkdown({site: 'scifi', entityId: 127968});
console.log(markdown);

deno run --allow-net deno-example.mjs printed the same sample text. Without --allow-net, and with no terminal to ask in, Deno refuses the request before anything is sent:

error: Uncaught (in promise) NotCapable: Requires net access to "api.stackexchange.com:443", run again with the --allow-net flag

--allow-net=api.stackexchange.com is the narrowest grant (not tested here; the run above used --allow-net).

Run it from npm without installing

$ npx stack-exchange-markdown-retriever@2.0.0 -s scifi 127968
Sample question body, made up for this wiki (not the real post).

It keeps HTML entities: &quot;quoted&quot; &amp; more.

    an indented code line
$ echo $?
0

With npm 11.16.0 the flags after the package name reached the tool: -s and -a were not taken by npx.

$ npx stack-exchange-markdown-retriever@2.0.0 -a 1010
Sample answer, made up for this wiki, with a [link](https://example.com/).
$ echo $?
0

Both outputs are sample content. The command itself is on the Commands page.

TypeScript

The package ships declarations for both builds (dist/index.d.mts and dist/index.d.cts). The Promise form resolves string | null, so TypeScript makes you handle a missing post. This file:

import {retrieveMarkdown} from 'stack-exchange-markdown-retriever';

const markdown = await retrieveMarkdown({entityId: 127968, site: 'scifi'});
console.log(markdown.length);

compiled with TypeScript 6.0.3 (tsc --noEmit --strict --module nodenext --moduleResolution nodenext types.mts) gives:

types.mts(4,13): error TS18047: 'markdown' is possibly 'null'.

The exported types are RetrieveMarkdownOptions, RetrieveMarkdownCallback and StackExchangeErrorDetails; see the API reference.

Browsers and workers

The README says the library runs in browsers and workers because it uses only fetch and other web platform APIs. Not tested for this wiki. In a browser the API's CORS headers decide whether the request is allowed, and an API key you pass is visible to your users.

Clone this wiki locally