Unauthorized

UNAUTHORIZED comes with HTTP 401. The API key is missing, or it is not a valid key.


What it means

Every REST request needs your API key in the X-API-KEY header. Without a key, or with a key that does not exist, the API stops before it does anything else. The request uses no credit.

The answer is the same for a missing and for a wrong key.

Example response

cURL

curl -X POST 'https://api.finlight.me/v3/articles/search' \
  -H 'Content-Type: application/json' \
  -H 'X-Request-Id: req-123' \
  -d '{}'

Response (401)

{
  "type": "https://docs.finlight.me/errors/unauthorized",
  "title": "Unauthorized",
  "status": 401,
  "detail": "Unauthorized",
  "code": "UNAUTHORIZED",
  "requestId": "req-123"
}

Common causes

  • The X-API-KEY header is missing.
  • The key has a typo, extra spaces or quotes, or was cut off when it was copied.
  • The key was deleted or replaced in the dashboard.
  • An environment variable is empty, so an empty key is sent.

How to fix

  1. Copy your key again from the finlight dashboard.
  2. Send it in the X-API-KEY header on every REST request.
  3. If you use a client library, pass the key when you create the client.

Send the key

curl -X POST 'https://api.finlight.me/v3/articles/search' \
  -H 'Content-Type: application/json' \
  -H 'X-API-KEY: YOUR_API_KEY' \
  -d '{ "query": "ticker:NVDA", "limit": 5 }'

See Authentication.

Retry

Do not retry. The same key fails again. The client libraries never retry a 401.

In the SDKs

LanguageError
TypeScript, Python, RubyUnauthorizedError
.NET, Java, PHPUnauthorizedException
Go*finlight.APIError with APIKind == finlight.APIKindUnauthorized
RustError::Api with api_kind() == ApiErrorKind::Unauthorized

Catch a key error

import { UnauthorizedError } from 'finlight-client'

try {
  await client.articles.search({ query: 'ticker:NVDA' })
} catch (err) {
  if (err instanceof UnauthorizedError) {
    console.error('Check FINLIGHT_API_KEY', err.requestId)
  } else {
    throw err
  }
}

On WebSocket

A WebSocket connection with a missing or wrong key is refused at connect with HTTP status 401. Many WebSocket libraries only show the status. The body, if your library shows it, is one of:

BodyCause
{"error":"Missing API-KEY"}No key was sent.
{"message":"Invalid API key"}The key does not exist.
{"message":"Invalid API key format"}The value does not look like a finlight key.

Do not reconnect with the same key. The client libraries end the stream with an unauthorized error.

On the WebSocket, the header name must be lower case: x-api-key. Some HTTP clients change header names to X-Api-Key on their own; then the key is not found and you get a 401 even with a valid key. In a browser, which cannot set headers, use ?apiKey=YOUR_API_KEY in the URL. See Connect.