First Perplexity API Call
First Perplexity API Call
Once you have your key, you can test it out. API calls are typically made from code. Here is a simplified example using the popular Python programming language and the `requests` library. This code sends a question to the API and asks for a response.
import requests
# Your unique API key goes here
API_KEY = 'pplx-YourGeneratedKeyHere12345'
# The URL for the Perplexity API endpoint
url = 'https://api.perplexity.ai/chat/completions'
# Configuring the request headers to include your API key for authorization
headers = {
'Authorization': f'Bearer {API_KEY}',
'Content-Type': 'application/json'
}
# The question you want to ask the AI
data = {
'model': 'sonar',
'messages': [
{'role': 'user', 'content': 'What is the TestingDocs YouTube
channel link'}
]
}
# Send the POST request to the API
response = requests.post(url, json=data, headers=headers)
# Print the AI's answer
print(response.json())
In the above code, the line `’Authorization’: f’Bearer {API_KEY}’` is important. This is where your secret API key is passed to the server to prove you are authorized to make the call. If the key is correct, the API will process your prompt and returns an intelligent answer.