# How to Use an API Key: curl, Python and JS

> How to use an API key: send it in the Authorization header as a Bearer token. Working calls in curl, Python, JavaScript and PowerShell, with real errors.

[Home](https://quanticdata.io/) / [Blog](https://quanticdata.io/blog/) / How to Use an API Key: curl, Python and JS

# How to use an API key: the Authorization header in curl, Python, JavaScript and PowerShell

Web scraping · Nov 20, 2026 · 6 min read · By [Aldo Morese](https://quanticdata.io/about/), founder of QuanticData

![Using an API key in three steps: store the key in an environment variable, send it in the Authorization header as a Bearer token, read the JSON answer; the card shows that the same call returned the page title from curl, Python, JavaScript and PowerShell, and that a request with no header answers 401.](https://quanticdata.io/blog/how-to-use-an-api-key/hero.webp?v=20261120h)

On this page

- [What an API key is, in three sentences](https://quanticdata.io/blog/how-to-use-an-api-key/#what-an-api-key-is-in-three-sentences)
- [Where the key goes](https://quanticdata.io/blog/how-to-use-an-api-key/#where-the-key-goes)
- [Step 1: put the key in an environment variable](https://quanticdata.io/blog/how-to-use-an-api-key/#step-1-put-the-key-in-an-environment-variable)
- [Step 2: make the first call](https://quanticdata.io/blog/how-to-use-an-api-key/#step-2-make-the-first-call)
- [Step 3: read the answer](https://quanticdata.io/blog/how-to-use-an-api-key/#step-3-read-the-answer)
- [When the key is not accepted: 401 and what it says](https://quanticdata.io/blog/how-to-use-an-api-key/#when-the-key-is-not-accepted-401-and-what-it-says)
- [Keep the key safe](https://quanticdata.io/blog/how-to-use-an-api-key/#keep-the-key-safe)
- [The same key in tools that are not code](https://quanticdata.io/blog/how-to-use-an-api-key/#the-same-key-in-tools-that-are-not-code)

To use an API key, send it with every request in the place the API's documentation names, most often the `Authorization` header: `Authorization: Bearer YOUR_KEY`. You do not install anything for the key itself. Any tool that can make an HTTP request can use it: curl, Python, JavaScript, PowerShell, Postman or a no-code workflow.

**Run it on the free credit.** Every QuanticData account gets $2 of free API usage per month.

[Start free — $2/month included](https://quanticdata.io/signup/?utm_source=blog&utm_medium=website&utm_campaign=blog-lead&utm_content=how-to-use-an-api-key) · [Explore Web Scraping API](https://quanticdata.io/web-scraping-api/)

The calls below were run on 20 November 2026 against the QuanticData [web scraping API](https://quanticdata.io/web-scraping-api/), which reads the key from the `Authorization` header. The same steps apply to any API that uses Bearer keys; only the address and the body change.

## What an API key is, in three sentences

An API key is a long random string that identifies your account to a service. The service uses it to decide what you may call, to count your usage and to bill you. Anyone who has the string can call the API as you, so it is handled like a password.

A QuanticData key starts with `qd_live_` followed by 64 hexadecimal characters. It is created on the API keys page of the dashboard and shown once, at creation: copy it then. If you do not have a key yet, the steps are in [how to create a SERP API key](https://quanticdata.io/blog/how-to-create-a-serp-api-key/).

## Where the key goes

| Place | Looks like | Used by |
| --- | --- | --- |
| Authorization header, Bearer scheme | `Authorization: Bearer KEY` | Most modern APIs, QuanticData included |
| A custom header | `X-API-Key: KEY` | Some APIs; check their docs |
| The query string | `?api_key=KEY` | Older APIs. URLs end up in logs and browser history, so prefer a header when the API offers one |

An API accepts the key only where it says it does. The Data API reads the key from the `Authorization` header: there is no `x-api-key` alternative there, and keys are not accepted in the URL. That keeps the key out of server logs and shared links.

## Step 1: put the key in an environment variable

Do this before the first call, so the key never appears in a script or in your shell history as part of a command.

```bash
# macOS, Linux, Git Bash
export QD_API_KEY="qd_live_your_key_here"

# Windows PowerShell (current window)
$env:QD_API_KEY = "qd_live_your_key_here"
```

## Step 2: make the first call

This request asks the API to fetch a public practice page and return it as Markdown. It is the same in every language: a POST, two headers, a small JSON body.

### curl

```bash
$ curl -s https://api.quanticdata.io/v1/scraper/extract \
    -H "Authorization: Bearer $QD_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{"url":"https://books.toscrape.com/","format":"markdown"}'
{"type":"response","message":"Extraction successful","payload":{"url":"https://books.toscrape.com/",
 "status":200,"contentType":"text/html","format":"markdown","title":"All products | Books to Scrape - Sandbox",
 "content":"[Books to Scrape](https://books.toscrape.com/index.html) We love being scraped!\n\n ...", ...}}
```

curl is already on Windows 10 and 11, macOS and Linux. In Windows PowerShell type `curl.exe`, because `curl` alone is an alias for a different command there. Header syntax is explained in [curl headers](https://quanticdata.io/blog/curl-headers/).

### Python

```python
import os, requests

r = requests.post(
    "https://api.quanticdata.io/v1/scraper/extract",
    headers={"Authorization": "Bearer " + os.environ["QD_API_KEY"]},
    json={"url": "https://books.toscrape.com/", "format": "markdown"},
    timeout=90,
)
body = r.json()
print(r.status_code, body["type"], body["payload"]["title"])
```

```
200 response All products | Books to Scrape - Sandbox
```

### JavaScript (Node.js 18 or later)

```javascript
const res = await fetch("https://api.quanticdata.io/v1/scraper/extract", {
  method: "POST",
  headers: { Authorization: "Bearer " + process.env.QD_API_KEY, "Content-Type": "application/json" },
  body: JSON.stringify({ url: "https://books.toscrape.com/", format: "markdown" }),
});
const body = await res.json();
console.log(res.status, body.type, body.payload.title);
```

```
200 response All products | Books to Scrape - Sandbox
```

Save it as a `.mjs` file and run it with `node`. This code belongs on a server or in a script. Do not put a key in JavaScript that runs in a visitor's browser, where anyone can read it; call your own backend from the browser and let the backend add the header, as in [a Supabase Edge Function](https://quanticdata.io/blog/supabase-edge-function-web-scraping/).

### PowerShell

```
$headers = @{ Authorization = "Bearer $env:QD_API_KEY" }
$body = @{ url = "https://books.toscrape.com/"; format = "markdown" } | ConvertTo-Json
$r = Invoke-RestMethod -Method Post -Uri "https://api.quanticdata.io/v1/scraper/extract" -Headers $headers -ContentType "application/json" -Body $body
"$($r.type) | $($r.payload.title)"
```

```
response | All products | Books to Scrape - Sandbox
```

**Try it on your own targets.** Every QuanticData account gets $2 of free API usage per month.

[Start free — $2/month included](https://quanticdata.io/signup/?utm_source=blog&utm_medium=website&utm_campaign=blog-inline&utm_content=how-to-use-an-api-key) · [Explore Web Scraping API](https://quanticdata.io/web-scraping-api/)

## Step 3: read the answer

Every answer from this API has the same three parts, so one piece of code can handle them all:

- **`type`**: `"response"` when the call worked, `"error"` when it did not.
- **`message`**: a sentence that says what happened.
- **`payload`**: the data, or details of the error.

Check `type` (or the HTTP status) before you use `payload`.

## When the key is not accepted: 401 and what it says

We sent three wrong requests on purpose. Each answered 401, and the message names the problem:

| What we sent | Message | Fix |
| --- | --- | --- |
| No `Authorization` header | Missing or invalid Authorization header. Use: Authorization: Bearer YOUR_API_KEY | Add the header; check the variable is set in this shell |
| The key in an `x-api-key` header | The same message | Move the key to `Authorization: Bearer` |
| `Bearer` with a string that is not a key | Invalid API key format | Paste the whole key: prefix plus 64 characters, no quotes or spaces inside |

The usual causes behind them are small: the environment variable was set in another terminal window, the word `Bearer` or the space after it is missing, or the key was cut when it was copied. Print the length of the variable, not the key, to check: a full key is 72 characters.

Other status codes are not about the key. A 402 means the account's free usage and balance do not cover the call, and a 429 means too many requests in a short time: wait the seconds in the [Retry-After header](https://quanticdata.io/blog/retry-after-header/) and continue.

## Keep the key safe

- **Environment variables or a secrets manager**, never the source code. If the project has a `.env` file, add it to `.gitignore` before the first commit.
- **One key per application.** When something leaks or a project ends, you revoke that key and nothing else stops.
- **Server side only.** No keys in web pages, mobile apps or public notebooks.
- **If a key is exposed**, disable it on the API keys page and create a new one. A disabled key answers 401.
- **Do not paste a key into a chat or a form** that asks for it.

## The same key in tools that are not code

- **Postman and similar clients:** choose the Bearer Token authorization type and paste the key; the client writes the header.
- **No-code workflows** (n8n, Logic Apps, Zapier): add a header named `Authorization` with the value `Bearer`, a space and the key, stored as a credential.
- **AI agents:** the [MCP server](https://quanticdata.io/mcp-server/) can run with the key in an environment variable, or connect with a sign-in and no key to paste.

From here, every endpoint works the same way: same header, a different address and body. The full list, with examples in four languages, is in the [API reference](https://quanticdata.io/docs/).

### Sources & further reading

- [RFC 6750: The OAuth 2.0 Authorization Framework, Bearer Token Usage](https://www.rfc-editor.org/rfc/rfc6750)
- [MDN: Authorization header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Authorization)
- [RFC 9110: 401 Unauthorized](https://www.rfc-editor.org/rfc/rfc9110#status.401)
- [Requests: Quickstart, custom headers](https://requests.readthedocs.io/en/latest/user/quickstart/#custom-headers)
- [Books to Scrape (practice site)](https://books.toscrape.com/)

## FAQ

Quick answers on how to use an API key.

[Something else? Ask us](mailto:hello@quanticdata.io)

### How do I use an API key?

Send it with every request where the API's documentation says, most often as `Authorization: Bearer YOUR_KEY`. In curl that is `-H "Authorization: Bearer $QD_API_KEY"`. Our test call returned the page title with status 200.

### Do I need to install anything to use an API key?

No. The key is a string in a header. curl ships with Windows, macOS and Linux; Python needs only the `requests` package; Node.js 18 or later, Deno and Bun have `fetch` built in.

### How do I use an API key in Python?

Read it from an environment variable and pass it in the headers: `requests.post(url, headers={"Authorization": "Bearer " + os.environ["QD_API_KEY"]}, json=...)`.

### Should the API key go in the URL or in a header?

In a header when the API supports it. URLs are stored in logs, browser history and shared links. The QuanticData Data API reads the key from the `Authorization` header.

### What is the difference between X-API-Key and Authorization: Bearer?

They are two header conventions for the same job, and each API chooses one. Sent to the wrong one, the key is ignored: our request with `x-api-key` answered 401 "Missing or invalid Authorization header".

### Why do I get 401 with a valid API key?

Check that the header is exactly `Authorization: Bearer KEY` with one space, that the environment variable is set in the same terminal, and that the key was copied whole (72 characters for a QuanticData key). A disabled or expired key also answers 401.

## One key for every endpoint

Scrape, search, crawl and run collectors with the same Bearer key and the same JSON envelope.

[Start free — $2/month included](https://quanticdata.io/signup/?utm_source=blog&utm_medium=website&utm_campaign=blog-end&utm_content=how-to-use-an-api-key) · [Explore Web Scraping API](https://quanticdata.io/web-scraping-api/)

## Related reading

- [Screen Scraping: Meaning, Example, vs API](https://quanticdata.io/blog/screen-scraping/): Web scraping · Screen scraping reads data from what a program shows on screen instead of from an API or the page source. We read the same ten records from one public page three ways through a residential proxy: off the rendered screen, out of the HTML, and from the JSON API.
- [Web Scraping Project Ideas, Started for Real](https://quanticdata.io/blog/web-scraping-project-ideas/): Web scraping · Five web scraping project ideas, each one started for real on 20 November 2026: a price tracker, a library release dashboard, a question analysis, an open-source landscape and a topic timeline, with the code, the first run and what to build next.
- [Retry-After Header: Read It, Wait, Retry](https://quanticdata.io/blog/retry-after-header/): Web scraping · Retry-After tells a client how long to wait before the next request, as a number of seconds or as an HTTP date. Below: both formats, a Python and a Node helper we ran against a real 429 on 20 November 2026 (each waited 10 seconds and then got 200), the rate-limit headers of the QuanticData API, and how one batch job replaced 20 calls.

---

Source: https://quanticdata.io/blog/how-to-use-an-api-key/ · Site index for AI: https://quanticdata.io/llms.txt
