Portkey Docs
HomeAPIIntegrationsChangelog
  • Introduction
    • What is Portkey?
    • Make Your First Request
    • Feature Overview
  • Integrations
    • LLMs
      • OpenAI
        • Structured Outputs
        • Prompt Caching
      • Anthropic
        • Prompt Caching
      • Google Gemini
      • Groq
      • Azure OpenAI
      • AWS Bedrock
      • Google Vertex AI
      • Bring Your Own LLM
      • AI21
      • Anyscale
      • Cerebras
      • Cohere
      • Fireworks
      • Deepbricks
      • Deepgram
      • Deepinfra
      • Deepseek
      • Google Palm
      • Huggingface
      • Inference.net
      • Jina AI
      • Lingyi (01.ai)
      • LocalAI
      • Mistral AI
      • Monster API
      • Moonshot
      • Nomic
      • Novita AI
      • Ollama
      • OpenRouter
      • Perplexity AI
      • Predibase
      • Reka AI
      • SambaNova
      • Segmind
      • SiliconFlow
      • Stability AI
      • Together AI
      • Voyage AI
      • Workers AI
      • ZhipuAI / ChatGLM / BigModel
      • Suggest a new integration!
    • Agents
      • Autogen
      • Control Flow
      • CrewAI
      • Langchain Agents
      • LlamaIndex
      • Phidata
      • Bring Your own Agents
    • Libraries
      • Autogen
      • DSPy
      • Instructor
      • Langchain (Python)
      • Langchain (JS/TS)
      • LlamaIndex (Python)
      • LibreChat
      • Promptfoo
      • Vercel
        • Vercel [Depricated]
  • Product
    • Observability (OpenTelemetry)
      • Logs
      • Tracing
      • Analytics
      • Feedback
      • Metadata
      • Filters
      • Logs Export
      • Budget Limits
    • AI Gateway
      • Universal API
      • Configs
      • Multimodal Capabilities
        • Image Generation
        • Function Calling
        • Vision
        • Speech-to-Text
        • Text-to-Speech
      • Cache (Simple & Semantic)
      • Fallbacks
      • Automatic Retries
      • Load Balancing
      • Conditional Routing
      • Request Timeouts
      • Canary Testing
      • Virtual Keys
        • Budget Limits
    • Prompt Library
      • Prompt Templates
      • Prompt Partials
      • Retrieve Prompts
      • Advanced Prompting with JSON Mode
    • Guardrails
      • List of Guardrail Checks
        • Patronus AI
        • Aporia
        • Pillar
        • Bring Your Own Guardrails
      • Creating Raw Guardrails (in JSON)
    • Autonomous Fine-tuning
    • Enterprise Offering
      • Org Management
        • Organizations
        • Workspaces
        • User Roles & Permissions
        • API Keys (AuthN and AuthZ)
      • Access Control Management
      • Budget Limits
      • Security @ Portkey
      • Logs Export
      • Private Cloud Deployments
        • Architecture
        • AWS
        • GCP
        • Azure
        • Cloudflare Workers
        • F5 App Stack
      • Components
        • Log Store
          • MongoDB
    • Open Source
    • Portkey Pro & Enterprise Plans
  • API Reference
    • Introduction
    • Authentication
    • OpenAPI Specification
    • Headers
    • Response Schema
    • Gateway Config Object
    • SDK
  • Provider Endpoints
    • Supported Providers
    • Chat
    • Embeddings
    • Images
      • Create Image
      • Create Image Edit
      • Create Image Variation
    • Audio
      • Create Speech
      • Create Transcription
      • Create Translation
    • Fine-tuning
      • Create Fine-tuning Job
      • List Fine-tuning Jobs
      • Retrieve Fine-tuning Job
      • List Fine-tuning Events
      • List Fine-tuning Checkpoints
      • Cancel Fine-tuning
    • Batch
      • Create Batch
      • List Batch
      • Retrieve Batch
      • Cancel Batch
    • Files
      • Upload File
      • List Files
      • Retrieve File
      • Retrieve File Content
      • Delete File
    • Moderations
    • Assistants API
      • Assistants
        • Create Assistant
        • List Assistants
        • Retrieve Assistant
        • Modify Assistant
        • Delete Assistant
      • Threads
        • Create Thread
        • Retrieve Thread
        • Modify Thread
        • Delete Thread
      • Messages
        • Create Message
        • List Messages
        • Retrieve Message
        • Modify Message
        • Delete Message
      • Runs
        • Create Run
        • Create Thread and Run
        • List Runs
        • Retrieve Run
        • Modify Run
        • Submit Tool Outputs to Run
        • Cancel Run
      • Run Steps
        • List Run Steps
        • Retrieve Run Steps
    • Completions
    • Gateway for Other API Endpoints
  • Portkey Endpoints
    • Configs
      • Create Config
      • List Configs
      • Retrieve Config
      • Update Config
    • Feedback
      • Create Feedback
      • Update Feedback
    • Guardrails
    • Logs
      • Insert a Log
      • Log Exports [BETA]
        • Retrieve a Log Export
        • Update a Log Export
        • List Log Exports
        • Create a Log Export
        • Start a Log Export
        • Cancel a Log Export
        • Download a Log Export
    • Prompts
      • Prompt Completion
      • Render
    • Virtual Keys
      • Create Virtual Key
      • List Virtual Keys
      • Retrieve Virtual Key
      • Update Virtual Key
      • Delete Virtual Key
    • Analytics
      • Graphs - Time Series Data
        • Get Requests Data
        • Get Cost Data
        • Get Latency Data
        • Get Tokens Data
        • Get Users Data
        • Get Requests per User
        • Get Errors Data
        • Get Error Rate Data
        • Get Status Code Data
        • Get Unique Status Code Data
        • Get Rescued Requests Data
        • Get Cache Hit Rate Data
        • Get Cache Hit Latency Data
        • Get Feedback Data
        • Get Feedback Score Distribution Data
        • Get Weighted Feeback Data
        • Get Feedback Per AI Models
      • Summary
        • Get All Cache Data
      • Groups - Paginated Data
        • Get User Grouped Data
        • Get Model Grouped Data
        • Get Metadata Grouped Data
    • API Keys [BETA]
      • Update API Key
      • Create API Key
      • Delete an API Key
      • Retrieve an API Key
      • List API Keys
    • Admin
      • Users
        • Retrieve a User
        • Retrieve All Users
        • Update a User
        • Remove a User
      • User Invites
        • Invite a User
        • Retrieve an Invite
        • Retrieve All User Invites
        • Delete a User Invite
      • Workspaces
        • Create Workspace
        • Retrieve All Workspaces
        • Retrieve a Workspace
        • Update Workspace
        • Delete a Workspace
      • Workspace Members
        • Add a Workspace Member
        • Retrieve All Workspace Members
        • Retrieve a Workspace Member
        • Update Workspace Member
        • Remove Workspace Member
  • Guides
    • Getting Started
      • A/B Test Prompts and Models
      • Tackling Rate Limiting
      • Function Calling
      • Image Generation
      • Getting started with AI Gateway
      • Llama 3 on Groq
      • Return Repeat Requests from Cache
      • Trigger Automatic Retries on LLM Failures
      • 101 on Portkey's Gateway Configs
    • Integrations
      • Llama 3 on Portkey + Together AI
      • Introduction to GPT-4o
      • Anyscale
      • Mistral
      • Vercel AI
      • Deepinfra
      • Groq
      • Langchain
      • Mixtral 8x22b
      • Segmind
    • Use Cases
      • Few-Shot Prompting
      • Enforcing JSON Schema with Anyscale & Together
      • Detecting Emotions with GPT-4o
      • Build an article suggestion app with Supabase pgvector, and Portkey
      • Setting up resilient Load balancers with failure-mitigating Fallbacks
      • Run Portkey on Prompts from Langchain Hub
      • Smart Fallback with Model-Optimized Prompts
      • How to use OpenAI SDK with Portkey Prompt Templates
      • Setup OpenAI -> Azure OpenAI Fallback
      • Fallback from SDXL to Dall-e-3
      • Comparing Top10 LMSYS Models with Portkey
      • Build a chatbot using Portkey's Prompt Templates
  • Support
    • Contact Us
    • Developer Forum
    • Common Errors & Resolutions
    • December '23 Migration
    • Changelog
Powered by GitBook
On this page
  • Install the Portkey SDK
  • Export Portkey API Key
  • Basic Client Setup
  • Making a Request
  • Passing Trace ID or Metadata
  • Async Usage
  • Parameters

Was this helpful?

Edit on GitHub
  1. API Reference

SDK

PreviousGateway Config ObjectNextSupported Providers

Last updated 10 months ago

Was this helpful?

The Portkey SDK client enables various features of Portkey in an easy to use config-as-code paradigm.

Install the Portkey SDK

Add the Portkey SDK to your application to interact with Portkey's gateway.

npm install --save portkey-ai
pip install portkey-ai

Export Portkey API Key

export PORTKEY_API_KEY=""

Basic Client Setup

The basic Portkey SDK client needs 2 required parameters

  1. The Portkey Account's API key to authenticate all your requests

  2. The of the AI provider you want to use OR The being used

This is achieved through headers when you're using the REST API.

For example,

import Portkey from 'portkey-ai';

// Construct a client with a virtual key
const portkey = new Portkey({
    apiKey: "PORTKEY_API_KEY",
    virtualKey: "VIRTUAL_KEY"
})

// Construct a client with a config id
const portkey = new Portkey({
    apiKey: "PORTKEY_API_KEY",
    config: "cf-***" // Supports a string config slug or a config object
})

Find more info on what's available through .

from portkey_ai import Portkey

# Construct a client with a virtual key
portkey = Portkey(
    api_key="PORTKEY_API_KEY",
    virtual_key="VIRTUAL_KEY"
)

# Construct a client with provider and provider API key
portkey = Portkey(
    api_key="PORTKEY_API_KEY",
    config="cf-***" # Supports a string config slug or a config object
)

Find more info on what's available through .

curl https://api.portkey.ai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "x-portkey-api-key: $PORTKEY_API_KEY" \
  -H "x-portkey-virtual-key: $VIRTUAL_KEY" \ 
  -d '{
    "model": "gpt-3.5-turbo",
    "messages": [{"role": "user","content": "Hello!"}]
  }'

curl https://api.portkey.ai/v1/chat/completions \
  -H 'Content-Type: application/json' \
  -H 'x-portkey-api-key: $PORTKEY_API_KEY' \
  -H 'x-portkey-config: cf-***' \ 
  -d '{
    "model": "gpt-3.5-turbo",
    "messages": [{"role": "user","content": "Hello!"}]
  }'

Making a Request

You can then use the client to make completion and other calls like this

const chatCompletion = await portkey.chat.completions.create({
    messages: [{ role: 'user', content: 'Say this is a test' }],
    model: 'gpt-3.5-turbo',
});

console.log(chatCompletion.choices);
completion = portkey.chat.completions.create(
    messages = [{ "role": 'user', "content": 'Say this is a test' }],
    model = 'gpt-3.5-turbo'
)

Passing Trace ID or Metadata

You can choose to override the configuration in individual requests as well and send trace id or metadata along with each request.

const chatCompletion = await portkey.chat.completions.create({
    messages: [{ role: 'user', content: 'Say this is a test' }],
    model: 'gpt-3.5-turbo',
}, {
    traceId: "39e2a60c-b47c-45d8", 
    metadata: {"_user": "432erf6"}
});

console.log(chatCompletion.choices);
completion = portkey.with_options(
    trace_id = "TRACE_ID", 
    metadata = {"_user": "USER_IDENTIFIER"}
).chat.completions.create(
    messages = [{ "role": 'user', "content": 'Say this is a test' }],
    model = 'gpt-3.5-turbo'
)

Async Usage

Portkey's Python SDK supports Async usage - just use AsyncPortkey instead of Portkey with await:

import asyncio
from portkey_ai import AsyncPortkey

portkey = AsyncPortkey(
    api_key="PORTKEY_API_KEY",
    virtual_key="VIRTUAL_KEY"
)

async def main():
    chat_completion = await portkey.chat.completions.create(
        messages=[{'role': 'user', 'content': 'Say this is a test'}],
        model='gpt-4'
    )

    print(chat_completion)

asyncio.run(main())

Parameters

Following are the parameter keys that you can add while creating the Portkey client.

Keeping in tune with the most popular language conventions, we use:

  • camelCase for Javascript keys

  • snake_case for Python keys

  • hyphenated-keys for the headers

Parameter
Type
Key

API Key Your Portkey account's API Key.

stringrequired

apiKey

Virtual Key The virtual key created from Portkey's vault for a specific provider

string

virtualKey

stringobject

config

string

provider

string

baseURL

Trace ID An ID you can pass to refer to 1 or more requests later on. Generated automatically for every request, if not sent.

string

traceID

Metadata Any metadata to attach to the requests. These can be filtered later on in the analytics and log dashboards Can contain _prompt, _user, _organisation, or _environment that are special metadata types in Portkey. You can also send any other keys as part of this object.

object

metadata

Cache Force Refresh Force refresh the cache for your request by making a new call and storing that value.

boolean

cacheForceRefresh

Cache Namespace Partition your cache based on custom strings, ignoring metadata and other headers.

string

cacheNamespace

Custom Host Route to locally or privately hosted model by configuring the API URL with custom host

string

customHost

Forward Headers Forward sensitive headers directly to your model's API without any processing from Portkey.

array of string

forwardHeaders

Azure OpenAI Headers Configuration headers for Azure OpenAI that you can send separately

string

azureResourceName azureDeploymentId azureApiVersion azureModelName

Google Vertex AI Headers Configuration headers for Vertex AI that you can send separately

string

vertexProjectId vertexRegion

AWS Bedrock Headers Configuration headers for Bedrock that you can send separately

string

awsAccessKeyId awsSecretAccessKey awsRegion awsSessionToken

Parameter
Type
Key

API Key Your Portkey account's API Key.

stringrequired

api_key

Virtual Key The virtual key created from Portkey's vault for a specific provider

string

virtual_key

stringobject

config

string

provider

string

base_url

Trace ID An ID you can pass to refer to 1 or more requests later on. Generated automatically for every request, if not sent.

string

trace_id

Metadata Any metadata to attach to the requests. These can be filtered later on in the analytics and log dashboards Can contain _prompt, _user, _organisation, or _environment that are special metadata types in Portkey. You can also send any other keys as part of this object.

object

metadata

Cache Force Refresh Force refresh the cache for your request by making a new call and storing that value.

boolean

cache_force_refresh

Cache Namespace Partition your cache based on custom strings, ignoring metadata and other headers.

string

cache_namespace

Custom Host Route to locally or privately hosted model by configuring the API URL with custom host

string

custom_host

Forward Headers Forward sensitive headers directly to your model's API without any processing from Portkey.

array of string

forward_headers

Azure OpenAI Headers Configuration headers for Azure OpenAI that you can send separately

string

azure_resource_name azure_deployment_id azure_api_version azure_model_name

Google Vertex AI Headers Configuration headers for Vertex AI that you can send separately

string

vertex_project_id vertex_region

AWS Bedrock Headers Configuration headers for Bedrock that you can send separately

string

aws_access_key_id aws_secret_access_key aws_region aws_session_token

Parameter
Type
Header Key

API Key Your Portkey account's API Key.

stringrequired

x-portkey-api-key

Virtual Key The virtual key created from Portkey's vault for a specific provider

string

x-portkey-virtual-key

string

x-portkey-config

string

x-portkey-provider

string

Change the request URL

Trace ID An ID you can pass to refer to 1 or more requests later on. Generated automatically for every request, if not sent.

string

x-portkey-trace-id

Metadata Any metadata to attach to the requests. These can be filtered later on in the analytics and log dashboards Can contain _prompt, _user, _organisation, or _environment that are special metadata types in Portkey. You can also send any other keys as part of this object.

string

x-portkey-metadata

Cache Force Refresh Force refresh the cache for your request by making a new call and storing that value.

boolean

x-portkey-cache-force-refresh

Cache Namespace Partition your cache based on custom strings, ignoring metadata and other headers

string

x-portkey-cache-namespace

Custom Host Route to locally or privately hosted model by configuring the API URL with custom host

string

x-portkey-custom-host

Forward Headers Forward sensitive headers directly to your model's API without any processing from Portkey.

array of string

x-portkey-forward-headers

Azure OpenAI Headers Configuration headers for Azure OpenAI that you can send separately

string

x-portkey-azure-resource-name x-portkey-azure-deployment-id x-portkey-azure-api-version api-key x-portkey-azure-model-name

Google Vertex AI Headers Configuration headers for Vertex AI that you can send separately

string

x-portkey-vertex-project-id x-portkey-vertex-region

AWS Bedrock Headers Configuration headers for Bedrock that you can send separately

string

x-portkey-aws-session-token x-portkey-aws-secret-access-key x-portkey-aws-region x-portkey-aws-session-token

Find more info on what's available through .

Config The slug or to use

Provider The AI provider to use for your calls. ().

Base URL You can edit the URL of the gateway to use. Needed if you're

Config The slug or to use

Provider The AI provider to use for your calls. ().

Base URL You can edit the URL of the gateway to use. Needed if you're

Config The slug or to use

Provider The AI provider to use for your calls. ().

Base URL You can edit the URL of the gateway to use. Needed if you're

configs here
config object
self-hosting the AI gateway
config object
self-hosting the AI gateway
config object
self-hosting the AI gateway
config
configs here
configs here
supported providers
supported providers
supported providers
virtual key