To consume a REST API from a script, curl fetches the response and jq reads
it; but without the --fail option, a 404 error exits with code 0 and the
script carries on as if everything were fine. This lesson teaches the steps
that make an API call reliable: reading the HTTP code, failing cleanly, passing
a token without exposing it, following pagination and sending a JSON body. The
examples query GitHub's public API, without authentication. Examples tested
with curl 8.18.0 and jq 1.8.2.
What you will learn
- Read the HTTP code of a response and make a script fail on an error.
- Pass an access token without exposing it.
- Follow an API's pagination with the Link header.
- Send a JSON body built by jq.
Prerequisites
Read the jq reference first, and the lesson Producing safe JSON from a script (in French). The principles of REST architecture (resources, methods, stateless server, cache) are presented in the section What are the principles of REST? of the lesson on REST APIs.
A first call, and what it hides
The simplest call reads the last three versions of yq and displays them with
jq. The -s (silent) and -S (still show errors) options form curl's basic
setting in scripts:
curl -sS 'https://api.github.com/repos/mikefarah/yq/releases?per_page=3' \ | jq -r '.[] | [.tag_name, .published_at] | @tsv'# v4.54.1 2026-09-29T04:22:30Z# v4.53.6 2026-08-20T09:39:39Z# v4.53.4 2026-08-19T05:21:13ZWhat you cannot see is the HTTP code. The -w (write-out) option prints it
after the response, along with other useful information such as the content
type:
curl -sS -o rel.json -w '%{http_code} %{content_type}\n' \ 'https://api.github.com/repos/mikefarah/yq/releases?per_page=3'# 200 application/json; charset=utf-8The trap: an HTTP error exits with code 0
With no option, curl considers that a received response is a success, whatever its HTTP code. A nonexistent resource returns a 404 with a JSON error body, and curl exits with 0: the script carries on, and jq reads the error as data.
curl -sS https://api.github.com/repos/mikefarah/nexiste-pas | jq -c .# {"message":"Not Found","documentation_url":"...","status":"404"}echo $? # 0The --fail option makes curl exit with 22 on any HTTP code of 400 or above,
without printing the body. --fail-with-body does the same but keeps the
body, useful when the API explains the error in it:
curl -sS --fail https://api.github.com/repos/mikefarah/nexiste-pas -o /dev/null# curl: (22) The requested URL returned error: 404curl -sS --fail-with-body https://api.github.com/repos/mikefarah/nexiste-pas | jq -r .message# Not Found (and curl exits with 22)Rule to remember: in a script, an API call always carries --fail or
--fail-with-body, and the script stops on the error (set -euo pipefail).
Likewise, --retry 2 retries a transient error such as a 503, but without
--fail, the final failure still exits with code 0.
Pass a token without exposing it
APIs that require authentication often expect a token in the Authorization
header. Passing it in clear on the command line, with
-H "Authorization: Bearer $TOKEN", makes it visible in the process list
(ps) and sometimes in the shell history.
curl can read its headers from a file, with -H @file. Created with
restricted permissions, this file reveals nothing:
umask 077printf 'Authorization: Bearer %s\nAccept: application/json\n' "$TOKEN" > entetes.txtcurl -sS --fail -H @entetes.txt https://api.exemple.fr/v1/serveursThe file is created with mode 0600, readable by you alone. To see what the
server receives, with a dummy token, send the same request to the httpbin.org
echo service, which returns the headers it received:
TOKEN=jeton-factice-123(umask 077; printf 'Authorization: Bearer %s\nAccept: application/json\n' "$TOKEN" > entetes.txt)curl -sS --fail -H @entetes.txt https://httpbin.org/headers \ | jq -c '.headers | {Authorization, Accept}'# {"Authorization":"Bearer jeton-factice-123","Accept":"application/json"}rm entetes.txtThe parentheses limit umask 077 to the creation of the file: without them,
it stays active for the whole terminal session. In a script, delete the file at
the end of the run, or put it in a temporary directory that is cleaned up
automatically.
Follow pagination
An API rarely returns a long list in one go: it splits it into pages.
GitHub, like many APIs, gives the next page in the Link header, with the
rel="next" relation:
curl -sS -I 'https://api.github.com/repos/mikefarah/yq/releases?per_page=3' | grep -i '^link:'# link: <https://api.github.com/repositories/43225113/releases?per_page=3&page=2>; rel="next", <...&page=57>; rel="last"A script follows these links until there are none left, reading the headers
of each response into a file with -D (dump-header). Limited here to three
pages for the example:
url='https://api.github.com/repos/mikefarah/yq/tags?per_page=50'; n=0; pages=0while [ -n "$url" ] && [ "$pages" -lt 3 ]; do entetes=$(mktemp) n=$(( n + $(curl -sS --fail -D "$entetes" "$url" | jq 'length') )) url=$(grep -i '^link:' "$entetes" | grep -o '<[^>]*>; rel="next"' | sed 's/[<>]//g; s/; rel="next"//') pages=$((pages + 1)); rm -f "$entetes"doneecho "tags read over $pages pages: $n"# tags read over 3 pages: 150Unauthenticated APIs also limit the number of calls: GitHub reports it in
the x-ratelimit-remaining header. A script that paginates a lot must watch
it, or authenticate to get a higher limit.
Send a JSON body
To create or modify a resource, you send a JSON body with the POST or PATCH
method. Built with jq -n --arg, it stays valid whatever the values, and
--data-binary @- reads it from standard input:
jq -nc --arg titre 'Bug "urgent"' --arg corps $'ligne 1\nligne 2' \ '{title: $titre, body: $corps}' \ | curl -sS --fail -X POST https://httpbin.org/post \ -H 'Content-Type: application/json' --data-binary @- \ | jq -c .json# {"body":"ligne 1\nligne 2","title":"Bug \"urgent\""}The Content-Type: application/json header tells the server how to read the
body: without it, many APIs refuse it or read it as a form. The echo
service returns the body it received, with quotes and line breaks intact.
Key takeaways
-sSis the basic setting in scripts;-w '%{http_code}'prints the HTTP code.- Without
--fail, a 404 exits with code 0: use--failor--fail-with-body, which exit with 22. - A token is passed through a header file with mode 0600
(
-H @file), never in clear on the command line. - Pagination is followed through the
Linkheader and itsrel="next"relation, read with-D. - A JSON body is built with
jq -n --argand sent with--data-binary @-andContent-Type: application/json.