Appwrite Function & NVIDIA NIM Setup
Proxy AI queries securely through an Appwrite Function serverless endpoint without exposing credentials to client code.
Zero-Hardcoding Security Policy
Never place API keys (like NVIDIA_API_KEY) directly in client JavaScript or commit them to Git repositories. Always store secrets in Appwrite Console > Functions > Settings > Variables and access them server-side via process.env.NVIDIA_API_KEY.
1. Architecture Flow
1. Frontend Browser
Posts prompt to Appwrite Function /v1/functions/ask-nvidia/executions.
2. Appwrite Function
Reads NVIDIA_API_KEY securely from server environment variables.
3. NVIDIA NIM API
Proxies to integrate.api.nvidia.com and returns markdown response.
2. Function File Structure
The Appwrite Function code lives inside the repository at functions/ask-nvidia/:
functions/
└── ask-nvidia/
├── package.json
└── src/
└── main.jsServerless Handler (`functions/ask-nvidia/src/main.js`)
export default async ({ req, res, log, error }) => {
const headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
'Access-Control-Allow-Methods': 'POST, OPTIONS',
};
if (req.method === 'OPTIONS') {
return res.empty(204, headers);
}
try {
// 1. Fetch secret key securely from server environment
const apiKey = process.env.NVIDIA_API_KEY || req.variables?.NVIDIA_API_KEY;
if (!apiKey) {
error('NVIDIA_API_KEY missing on server environment.');
return res.json({ success: false, error: 'NVIDIA_API_KEY is not configured on server.' }, 500, headers);
}
// 2. Parse request payload
let body = {};
try {
body = typeof req.body === 'string' ? JSON.parse(req.body) : req.body;
} catch (e) {
body = {};
}
const promptText = body.prompt || body.question;
if (!promptText) {
return res.json({ success: false, error: 'Prompt is required.' }, 400, headers);
}
const model = body.model || "deepseek-ai/deepseek-r1";
// 3. Proxy request to NVIDIA NIM Serverless Endpoint
const response = await fetch("https://integrate.api.nvidia.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${apiKey}`
},
body: JSON.stringify({
model: model,
messages: [
{ role: "system", content: "You are an expert Neovim tutor. Answer clearly with markdown code blocks." },
{ role: "user", content: promptText }
],
temperature: 0.2,
max_tokens: 2048
})
});
if (!response.ok) {
const errText = await response.text();
error(`NVIDIA API Error (${response.status}): ${errText}`);
return res.json({ success: false, error: `Upstream API error (${response.status})` }, response.status, headers);
}
const data = await response.json();
const reply = data.choices?.[0]?.message?.content || "No response generated.";
return res.json({ success: true, reply }, 200, headers);
} catch (err) {
error(`Unexpected Error: ${err.message}`);
return res.json({ success: false, error: err.message }, 500, headers);
}
};3. Deploying Function via Git
Appwrite Functions supports automatic deployment directly from your connected Git repository whenever you push commits.
- Open Appwrite Console: Go to Functions > Create Function.
- Select Git Integration: Click Connect Git Repository and select repository
RTFM. - Configure Production Settings:
Setting Field Value Description Function Name ask-nvidia Function identifier Runtime Node.js 20.x / 22.x Execution runtime environment Root Directory functions/ask-nvidia Directory containing main.js Entrypoint src/main.js Relative path to entry file Execute Access Any (Public) Allows website visitors to trigger AI Tutor - Save & Trigger Deploy: Click Deploy. Any future push to
mainwill trigger automatic function deployments!
4. Adding Appwrite Secrets (Console Config)
To give your serverless function access to NVIDIA NIM endpoints without putting keys in code:
- Navigate to Appwrite Console > Functions > ask-nvidia.
- Select the Settings tab $\rightarrow$ Scroll down to Variables.
- Click Add Variable:
- Key:
NVIDIA_API_KEY - Value: nvapi-YOUR_ACTUAL_NVIDIA_KEY_HERE
- Key:
- Click Update to save the environment variable.
process.env.NVIDIA_API_KEY.