Welcome to Portkey Forum

Updated last week

Using Portkey with Google API Requires Actual API Key

I am experimenting with using the OpenAI sdk (since we have other tooling that relies on it) but using portkey to access other models. Specifically for Google I would have expected the following code to work, but instead of working with the virtual key, I need to specify the actual API key for Google. Am I doing something wrong? Do virtual keys only work with the Portkey sdk?

This fails with BadRequestError: 400 google error: API key not valid. Please pass a valid API key.
Plain Text
const openai = new OpenAI({
  apiKey: process.env["GOOGLE_VIRTUAL_API_KEY"],
  baseURL: PORTKEY_GATEWAY_URL,
  defaultHeaders: createHeaders({
    provider: "google",
    apiKey: process.env["PORTKEY_API_KEY"],
  })
});


If I switch out the GOOGLE_VIRTUAL_API_KEY env var instead for GOOGLE_API_KEY it works.
C
k
W
4 comments
Hey!

In the code snippet shared by you, the apiKey is expected to have the API KEY of the provider and not the virtualKey

If you want it to work with virtualKey you can try this implementation.

Plain Text
const openai = new OpenAI({
    apiKey: "", <- Can be left like this
    baseURL: PORTKEY_GATEWAY_URL,
    defaultHeaders: createHeaders({
        apiKey: "PORKTKEY_API_KEY",
        virtualKey: "PROVIDER_VIRTUAL_KEY"
    })
});


Just notice that the apiKey in OpenAI client can be an empty string as well. Since this field is mandatory from the OpenAI SDK. It makes no difference tho.
That worked thanks 🥳
thanks a ton @kam for contributing to our docs. tagging @Vrushank | Portkey for reviewing this
Add a reply
Sign up and join the conversation on Discord