# Bare minimum example on website is wrong

**URL:** <https://community.perplexity.ai/t/bare-minimum-example-on-website-is-wrong/56>\
**Category:** General\
**Created:** [May 15, 2025, 12:11am UTC](https://community.perplexity.ai/t/bare-minimum-example-on-website-is-wrong/56 "2025-05-15T00:11:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vikvang](https://avatars.discourse-cdn.com/v4/letter/v/67e7ee/32.png) [@vikvang](https://community.perplexity.ai/u/vikvang)\
**Post date:** [May 15, 2025, 12:11am UTC](https://community.perplexity.ai/t/bare-minimum-example-on-website-is-wrong/56/1 "2025-05-15T00:11:20Z")

</div>

Hi,

The bare minimum [example](https://www.perplexity.ai/hub/blog/introducing-pplx-api) of querying the API via Python is not correct.

If you try the code below it will fail with an 401 Authorization error:

```auto
from openai import OpenAI

YOUR_API_KEY = "INSERT API KEY HERE"

messages = [
    {
        "role": "system",
        "content": (
            "You are an artificial intelligence assistant and you need to "
            "engage in a helpful, detailed, polite conversation with a user."
        ),
    },
    {
        "role": "user",
        "content": (
            "Count to 100, with a comma between each number and no newlines. "
            "E.g., 1, 2, 3, ..."
        ),
    },
]

client = OpenAI(api_key=YOUR_API_KEY, base_url="https://api.perplexity.ai")

# demo chat completion without streaming
response = client.chat.completions.create(
    model="mistral-7b-instruct",
    messages=messages,
)
print(response)

```

The example that should be shown instead is the one that features the addition of the `extra_headers` that allow this to work:

```auto
response = client.chat.completions.create(
    model="mistral-7b-instruct",
    messages=messages,
    extra_headers={"Authorization": f"Bearer {YOUR_API_KEY}"},
)

```

Looking forward to having this fixed as it is easy for beginners to trip here.

This also resulted in libraries like `litellm` and `magentic` not properly supporting adding a custom header for Perplextiy to work with custom keys that aren’t defined as environment variables.

---

<div class="post-metadata">

**Author:** ![vikvang](https://avatars.discourse-cdn.com/v4/letter/v/67e7ee/32.png) [@vikvang](https://community.perplexity.ai/u/vikvang)\
**Post date:** [May 15, 2025, 12:11am UTC](https://community.perplexity.ai/t/bare-minimum-example-on-website-is-wrong/56/2 "2025-05-15T00:11:22Z")

</div>

Did you use your real API key ?

---

<div class="post-metadata">

**Author:** ![vikvang](https://avatars.discourse-cdn.com/v4/letter/v/67e7ee/32.png) [@vikvang](https://community.perplexity.ai/u/vikvang)\
**Post date:** [May 15, 2025, 12:11am UTC](https://community.perplexity.ai/t/bare-minimum-example-on-website-is-wrong/56/3 "2025-05-15T00:11:25Z")

</div>

Auth error comes from not using a real API key.
