Skip to content

Instantly share code, notes, and snippets.

@rowntreerob
Created November 27, 2023 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rowntreerob/f2c4816f485f9ed915e5cd482f3728ed to your computer and use it in GitHub Desktop.
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
'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