Repository navigation
Commands
The package installs one command, stack-exchange-markdown-retriever (the file dist/cli.mjs). It prints the markdown of one question or answer to stdout. Install it globally with npm install --global stack-exchange-markdown-retriever, or run it without installing through npx (Getting started).
Every transcript below is the published 2.0.0's own output on Node 24.18.0, with the local stand-in answering for the API. Post texts are sample content made up for this wiki, and the error bodies came from the stand-in.
$ stack-exchange-markdown-retriever --help
Print the markdown source of a question or an answer on a Stack Exchange site.
Usage
$ stack-exchange-markdown-retriever [-a] [-s <site>] [-k <key>] [--timeout <ms>] <id>
Options
-a, --answer The id is an answer's, not a question's
-s, --site <site> The site: a short name (scifi) or a domain
(scifi.stackexchange.com); default stackoverflow
-k, --apiKey <key> An API key, which raises the daily request quota
--timeout <ms> Give up after this many milliseconds (default 30000)
-h, --help Show this help
-v, --version Show the version
Prints null when there is no such post. On Node, a proxy in HTTPS_PROXY is
used only when NODE_USE_ENV_PROXY=1 is set.
Exit codes
0 the markdown (or null) was printed
1 an error: bad arguments, the API's error, or the network's
Example
$ stack-exchange-markdown-retriever -s scifi.stackexchange.com 127968
$ echo $?
0The help starts and ends with an empty line. Its proxy sentence held on Node 24.18.0. Node 20.20.2 ignores NODE_USE_ENV_PROXY, so there a proxy needs the preload shown in Recipes.
| Option | Takes | What it does |
|---|---|---|
<id> |
a post id | The first positional argument. Further ones are ignored. Digits, or ids joined by ; (quote those in a shell). |
-a, --answer
|
nothing | Request /answers/<id> instead of /questions/<id>. |
-s, --site
|
a value | The site, short name or domain. Default stackoverflow. |
-k, --apiKey
|
a value | Sent as the API's key parameter. |
--timeout |
milliseconds | Default 30000. 0 means no timeout. |
-h, --help
|
nothing | Print the help above and exit 0. |
-v, --version
|
nothing | Print the version and exit 0. |
Options may also be written --site=scifi. The option is spelled --apiKey, with a capital K.
A question on another site (sample content; the real post is https://scifi.stackexchange.com/q/127968):
$ stack-exchange-markdown-retriever -s scifi 127968
Sample question body, made up for this wiki (not the real post).
It keeps HTML entities: "quoted" & more.
an indented code line
$ echo $?
0An answer on Stack Overflow (sample content; compare the real one at https://stackoverflow.com/a/1010):
$ stack-exchange-markdown-retriever -a 1010
Sample answer, made up for this wiki, with a [link](https://example.com/).
$ echo $?
0It requested:
GET https://api.stackexchange.com/2.2/answers/1010?order=asc&filter=!L_(I6pMIzdXP-hC1clc9EY&site=stackoverflow
With an API key (sample content):
$ stack-exchange-markdown-retriever -k abc123 1
Sample question one.
$ echo $?
0It requested:
GET https://api.stackexchange.com/2.2/questions/1?order=asc&filter=!L_(I6pMIzdXP-hC1clc9EY&key=abc123&site=stackoverflow
Several ids in one request print the first post the API lists (sample content):
$ stack-exchange-markdown-retriever '2;1'
Sample question two.
$ echo $?
0$ stack-exchange-markdown-retriever --version
2.0.0
$ echo $?
0The markdown goes to stdout exactly as the API sent it, followed by a newline. A missing post prints null and exits 0, and so does a post without body_markdown:
$ stack-exchange-markdown-retriever 404
null
$ echo $?
00 is a valid id string for the command line: it is sent, and a missing post prints null:
$ stack-exchange-markdown-retriever 0
null
$ echo $?
0Every error goes to stderr as one line, <name>: <message>, with no stack trace, and the exit code is 1. In the transcripts below the line is stderr.
The API's error (the stand-in's body):
$ stack-exchange-markdown-retriever 9002
StackExchangeError: too many requests from this IP, more requests available in 3600 seconds
$ echo $?
1A body that is not JSON, such as an HTML error page:
$ stack-exchange-markdown-retriever 9007
StackExchangeError: The response is not JSON
$ echo $?
1A dropped connection:
$ stack-exchange-markdown-retriever 9020
TypeError: fetch failed
$ echo $?
1A server that does not answer in time:
$ stack-exchange-markdown-retriever --timeout 300 9011
TimeoutError: The operation timed out.
$ echo $?
1The ids 9002, 9007, 9020 and 9011 are the stand-in's routes for those answers, not real posts.
No id:
$ stack-exchange-markdown-retriever
Error: Need an entity id to read
$ echo $?
1An id that is not a post id is refused before any request:
$ stack-exchange-markdown-retriever abc
TypeError: entityId must be a post id (digits, or ids joined by ";"), not "abc"
$ echo $?
1-s takes the next word as its value, so -s 1 leaves no id:
$ stack-exchange-markdown-retriever -s 1
Error: Need an entity id to read
$ echo $?
1-s or -k at the end, with no value:
$ stack-exchange-markdown-retriever -s
error: Option '-s, --site <value>' argument missing
$ echo $?
1
$ stack-exchange-markdown-retriever -k
error: Option '-k, --apiKey <value>' argument missing
$ echo $?
1A bad --timeout:
$ stack-exchange-markdown-retriever --timeout abc 1
error: --timeout takes a number of milliseconds, not "abc"
$ echo $?
1An unknown option:
$ stack-exchange-markdown-retriever --foo 1
error: Unknown option '--foo'. To specify a positional argument starting with a '-', place it at the end of the command after '--', as in '-- "--foo"
$ echo $?
1Through Git Bash, with the installed command on PATH, printing each exit code:
stack-exchange-markdown-retriever 404; echo "exit $?"; stack-exchange-markdown-retriever abc 2>&1; echo "exit $?"printed:
null
exit 0
TypeError: entityId must be a post id (digits, or ids joined by ";"), not "abc"
exit 1
A script can tell "no such post" from an error only by the output: both a missing post and success exit 0, so compare stdout with null.
This wiki describes stack-exchange-markdown-retriever 2.0.0 and was last updated on 2026-09-29. The library is MIT licensed. Report problems in the issues. On npm: stack-exchange-markdown-retriever.
Using it
The releases
Contributing
Elsewhere