Created
November 27, 2023 22:44
-
-
Save rowntreerob/f2c4816f485f9ed915e5cd482f3728ed to your computer and use it in GitHub Desktop.
proxy openai-api.requests so a class has access without needing their own api keys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
/* | |
* students / class post everything to this domain (including path intended for openai | |
* this acts as reverse proxy with proper open api token value applied | |
* responses flow back to original client | |
*/ | |
import express from 'express' | |
var app = express(); | |
import httpProxy from 'http-proxy' | |
var apiProxy = httpProxy.createProxyServer(); | |
const target = 'https://api.openai.com/' | |
const APIKEY = process.env.OPEN_API_KEY | |
const token = `Bearer ${APIKEY}` | |
// inject header w openai token and pass the request to target | |
apiProxy.on('proxyReq', function(proxyReq, req, res, options) { | |
proxyReq.setHeader('Authorization', token); | |
}); | |
app.post("/*", function(req, res) { | |
console.log('redirecting to Server2'); | |
apiProxy.web(req, res, {target: target, changeOrigin: true, secure: false}); | |
}); | |
app.listen(process.env.PORT || 5000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment