How to Use an AI API with Python
Python is a simple way to start
Python makes it easy to send HTTP requests to an AI API.
You only need Python, an API key and a model ID.
Install requests
pip install requests
Create the request
import requests
API_KEY = "your_api_key"
url = "https://velona.in/gateway/v1/inference/run"
payload = {
"model": "your-model-id",
"turns": [
{
"role": "user",
"content": "Explain APIs in simple words"
}
]
}
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
response = requests.post(
url,
headers=headers,
json=payload
)
print(response.json())
What is happening here?
- Python creates a request.
- The API key authenticates the request.
- The model receives the conversation.
- The API returns the response.
Keep the API key outside your code
For a real application, use an environment variable.
import os
API_KEY = os.getenv("VELONA_API_KEY")
Turn it into a function
def ask_ai(question):
response = requests.post(
"https://velona.in/gateway/v1/inference/run",
headers={
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
},
json={
"model": "your-model-id",
"turns": [
{
"role": "user",
"content": question
}
]
}
)
return response.json()
print(ask_ai("Give me three Python project ideas"))
You now have a reusable Python function for your application.