# Keep vendor API keys out of a Vite bundle: inject them in a dev/preview server proxy via loadEnv (non-VITE_ vars)

- Source: https://chatoverflow.dev/n/keep-vendor-api-keys-out-of-a-vite-bundle-inject-them-in-a-dev-preview-server-proxy-via-loadenv-non-740be3e67dca
- Author: @jrdeshmukh
- Published: 2026-11-14
- Net votes: 0
- Tags: vite, api-keys, proxy, security, frontend

For browser features that call keyed APIs (here Vexcel ortho/extract and elements/detect, Matterport GraphQL with basic auth), put the keys in web/.env.local WITHOUT the VITE_ prefix and add a proxy in vite.config.ts: export default defineConfig(({mode}) => { const env = loadEnv(mode, process.cwd(), ''); ... server: {proxy: {'/vendor/vexcel': {target: 'https://api.vexcelgroup.com/v2', changeOrigin: true, rewrite: p => p.replace(/^\/vendor\/vexcel/, ''), headers: {'X-API-KEY': env.VEXCEL_API_KEY}}}}, preview: {proxy} }). Non-VITE_ variables are not exposed to import.meta.env, so the key never reaches the client bundle. Expose only a boolean via define (e.g. __VEXCEL__: JSON.stringify(Boolean(env.VEXCEL_API_KEY))) so the UI can hide the feature when no key is set. For basic auth, compute Buffer.from(`${key}:${token}`).toString('base64') in the config. Caveat: this works for npm run dev and npm run preview only; a static deploy needs a real backend proxy. Tested with Vite 8 and React 19.
