Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🚀 feat: Add `OPENAI_ORGANIZATION` for all OpenAI Requests (#1590)

Danny Avilaa7c54573c4d4
diff --git a/.env.example b/.env.example
index 54301bebd2..7f49509c2d 100644
--- a/.env.example
+++ b/.env.example
@@ -115,6 +115,8 @@ DEBUG_OPENAI=false
# OPENAI_REVERSE_PROXY=
+# OPENAI_ORGANIZATION=
+
#============#
# OpenRouter #
#============#
diff --git a/api/app/clients/OpenAIClient.js b/api/app/clients/OpenAIClient.js
index f832b3ca13..7d0baa40d7 100644
--- a/api/app/clients/OpenAIClient.js
+++ b/api/app/clients/OpenAIClient.js
@@ -954,6 +954,10 @@ ${convo}
opts.defaultHeaders = { ...opts.defaultHeaders, 'api-key': this.apiKey };
}
+ if (process.env.OPENAI_ORGANIZATION) {
+ opts.organization = process.env.OPENAI_ORGANIZATION;
+ }
+
let chatCompletion;
const openai = new OpenAI({
apiKey: this.apiKey,
diff --git a/api/server/services/ModelService.js b/api/server/services/ModelService.js
index 2e433dbd14..76ac061546 100644
--- a/api/server/services/ModelService.js
+++ b/api/server/services/ModelService.js
@@ -53,6 +53,10 @@ const fetchModels = async ({ apiKey, baseURL, name = 'OpenAI', azure = false })
payload.httpsAgent = new HttpsProxyAgent(PROXY);
}
+ if (process.env.OPENAI_ORGANIZATION && baseURL.includes('openai')) {
+ payload.headers['OpenAI-Organization'] = process.env.OPENAI_ORGANIZATION;
+ }
+
const res = await axios.get(`${baseURL}${azure ? '' : '/models'}`, payload);
models = res.data.data.map((item) => item.id);
} catch (err) {
diff --git a/docs/install/configuration/dotenv.md b/docs/install/configuration/dotenv.md
index 09ade2a7f3..981656780d 100644
--- a/docs/install/configuration/dotenv.md
+++ b/docs/install/configuration/dotenv.md
@@ -285,6 +285,13 @@ GOOGLE_MODELS=gemini-pro,gemini-pro-vision,chat-bison,chat-bison-32k,codechat-bi
OPENAI_API_KEY=user_provided
```
+- You can specify which organization to use for each API request to OpenAI. However, it is not required if you are only part of a single organization or intend to use your default organization. You can check your [default organization here](https://platform.openai.com/account/api-keys). This can also help you limit your LibreChat instance from allowing API keys outside of your organization to be used, as a mismatch between key and organization will throw an API error.
+
+```bash
+# Optional
+OPENAI_ORGANIZATION=org-Y6rfake63IhVorgqfPQmGmgtId
+```
+
- Set to true to enable debug mode for the OpenAI endpoint
```bash