Getting Started
Este contenido aún no está disponible en tu idioma.
-
Install the package
Ventana de terminal npm i @capgo/capacitor-envVentana de terminal pnpm add @capgo/capacitor-envVentana de terminal yarn add @capgo/capacitor-envVentana de terminal bun add @capgo/capacitor-env -
Sync native platforms
Ventana de terminal npx cap syncVentana de terminal pnpm cap syncVentana de terminal yarn cap syncVentana de terminal bunx cap sync
Declare configuration values
Add keys to the Capacitor config so they are baked into your native builds. You can create multiple config variants (capacitor.config.prod.ts, capacitor.config.dev.ts, etc.) to swap values per environment.
import { CapacitorConfig } from '@capacitor/cli';
const config: CapacitorConfig = { appId: 'com.example.app', appName: 'Example', webDir: 'dist', plugins: { Env: { API_URL: 'https://api.example.com', PUBLIC_KEY: 'pk_live_123', }, },};
export default config;On native platforms the values are stored inside the generated configuration files (ios/App/App/capacitor.config.json and android/app/src/main/assets/capacitor.config.json). Update those files per flavor if you need tenant-specific values.
Read values in code
import { Env } from '@capgo/capacitor-env';
const apiUrl = await Env.getKey({ key: 'API_URL' }).then((result) => result.value);
if (!apiUrl) { throw new Error('Missing API_URL configuration');}Provide fallbacks
const loadConfig = async () => { const { value: endpoint } = await Env.getKey({ key: 'API_URL' }); return endpoint || 'https://staging.example.com';};Tips
- Use different
capacitor.configfiles per environment and point the CLI to the right one withnpx cap run ios --configuration=prod. - Combine with Capgo updater channels to ship tenant-specific values without publishing new binaries.
- Keep secrets out of source control by substituting them during your CI build before
npx cap sync.