Skip to content

Portkey AI

Using OpenAI Library

from openai import OpenAI
from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders

client = OpenAI(
    api_key="This-could-be-anything-not-used", 
    base_url=PORTKEY_GATEWAY_URL,
    default_headers=createHeaders(
        api_key="Your_Portkey_API_Key_Here",
        virtual_key="Your_Virtual_key_here",
        config="Your_config_slug_here",  # this is optional
    )
)

chat_complete = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Say this is a test"}],
)

Using Langchain Library:

from langchain_openai import ChatOpenAI, createHeaders
from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL

llm1 = ChatOpenAI(
    api_key="his-could-be-anything-not-used",
    model="mistral-large-latest",
    base_url=PORTKEY_GATEWAY_URL,
    default_headers=createHeaders(
        api_key="Your_Portkey_API_Key_Here",
        virtual_key="Your_Virtual_key_here",
        config="Your_config_slug_here",  # this is optional
    )
)
print(llm.invoke("What is the meaning of life, universe and everything?"))

Using curl command

curl -X POST https://api.portkey.ai/v1/chat/completions  \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY_HERE" \
-H "x-portkey-virtual-key: Your_Virtual_Key_Here" \
-d '{
    "model": "gpt-4.1-mini",
    "messages": [
        {
            "role": "user",
            "content": "Hello!"
        }
    ]
}'