Tavily API Authorization error

Hello everyone,

I’m facing an issue with a method I created to perform a web search using the Tavily API. Initially, the code was working fine, and I was receiving JSON responses. However, recently, even though there have been no changes made to the code interacting with the Tavily API, I started receiving the following error: {'detail': {'error': 'Unauthorized'}}.

Here’s a snippet of the method in question: ‘’’
headers = {“Content-Type”: “application/json”, “Authorization”: f"Bearer {self.api_key}"}

*def search(self, query: str, *kwargs) → dict:

  • api_url = “https://api.tavily.com” + “/search”*
  • data = {*
  •    "query": query,*
    
  •    **kwargs*
    
  • }*
  • response = requests.post(*
  •    url=api_url,*
    
  •    json=data,*
    
  •    headers=TavilyClient().headers,*
    
  •    timeout=100*
    
  • )*
  • return response.json()*
    ‘’’
    I have tried updating the API key, but the problem persists. Can anyone help me understand why this might be happening or suggest any potential fixes? Your assistance would be greatly appreciated!

Thank you in advance!
telegram: @vv10111

Hi there,

Based on the code snippet you shared, the problem might be related to how the headers are being handled when making the request.

Here’s an example implementation of the TavilyClient class that you can use to test and debug the issue:

import requests

class TavilyClient:
    def __init__(self, api_key: str):
        self.api_key = api_key
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": f"Bearer {self.api_key}"
        }

    def search(self, query: str, **kwargs) -> dict:
        api_url = "https://api.tavily.com/search"
        data = {
            "query": query,
            **kwargs
        }
        try:
            response = requests.post(
                url=api_url,
                json=data,
                headers=self.headers,
                timeout=100
            )
            response.raise_for_status() 
            return response.json()
        except requests.exceptions.RequestException as e:
            return {"error": str(e)}

if __name__ == "__main__":
    # Replace 'tvly-YOUR_API_KEY' with your actual API key
    client = TavilyClient(api_key="tvly-YOUR_API_KEY")
    
    # Example query
    test_query = "artificial intelligence"
    additional_params = {
        "max_results": 5,  # Example additional parameter
    }
    
    # Perform the search
    result = client.search(query=test_query, **additional_params)
    print(result)

In the provided example, the headers are directly tied to the instantiated object (self.headers), ensuring that the api_key is consistently applied across all requests.


Alternatively, you can simplify your code by using our tavily-python package. Here’s how:

  1. Install the package:
pip install tavily-python
  1. Use the following code:
from tavily import TavilyClient

# Instantiate the client with your API key
tavily_client = TavilyClient(api_key="tvly-YOUR_API_KEY")

# Perform a search query
response = tavily_client.search("Who is Leo Messi?")

# Print the response
print(response)

This approach ensures the headers and authentication are handled automatically by the package, simplifying your implementation. For more details, check out the Tavily Python SDK Documentation.

Let me know if this resolves your issue. We can explore further debugging steps! :blush:

May