Next.js Discord

Discord Forum

Vercel AI SDK - how do I use it with a JWT / Bearer Token?

Unanswered
Himalayan posted this in #help-forum
Open in Discord
HimalayanOP
I am currently building a ChatGPT chrome extension with react and typescript and I would like to use the Vercel AI SDK to easily stream the responses from ChatGPT.

From reading the documentation, it seems that I can only make the request with an API key:

const apiConfig = new Configuration({
 apiKey: process.env.OPENAI_API_KEY!,
})
 
const openai = new OpenAIApi(apiConfig)


My question is, would it possible to make requests with a valid JWT / Bearer Token from the current user session from ChatGPT? I want to implement this because it means they won't have to pay for the usage of a separate API key and they can just send requests as they would on the ChatGPT web app.

Thank you!

7 Replies

European sprat
Instead of reading it from an environment variable, you read it from wherever you're storing it
@European sprat Instead of reading it from an environment variable, you read it from wherever you're storing it
HimalayanOP
but i am wanting to pass a JWT instead of an API key, so this is my question - as i can't seem to find an option for this?
European sprat
If you're doing auth with a jwt then after the user has authenticated, you do a lookup in the DB or wherever else you're storing their API key and then use it
@European sprat If you're doing auth with a jwt then after the user has authenticated, you do a lookup in the DB or wherever else you're storing their API key and then use it
HimalayanOP
my goal is to not use any API key with the ai sdk, instead i would just like to perform the requests to openai backend with the JWT.

so ideal user flow is:

grab JWT from https://chat.openai.com/ session -> use that JWT within ai sdk -> be able to send messages and receive streamed response
European sprat
Oh I understand what you're trying to do now. It's not possible
@European sprat Oh I understand what you're trying to do now. It's not possible
HimalayanOP
if it's not possible with the vercel ai sdk, how do you suggest i handle the streaming of chatgpt responses manually?
European sprat
You use the API