0Pricing
AI Powered SaaS: Stripe + Auth + Billing + Deploy · Lezione

Variabili d’ambiente e gestione dei secrets

Configuri in sicurezza il Suo SaaS nei diversi ambienti gestendo i secrets, separando le variabili pubbliche da quelle private ed evitando di esporre le chiavi al client.

Variabili d’ambiente e gestione dei secrets è una lezione AI Powered SaaS: Stripe + Auth + Billing + Deploy gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento AI Powered SaaS: Stripe + Auth + Billing + Deploy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso AI Powered SaaS: Stripe + Auth + Billing + Deploy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

Why Configuration Matters

A SaaS connects to databases, payment providers, and AI APIs — each with keys that differ per environment. Environment variables keep them out of code so one codebase runs anywhere.

The .env File

Local config lives in a .env file as plain key-value pairs. It's loaded at startup and must never be committed to git.

DATABASE_URL=postgresql://localhost/app
STRIPE_SECRET_KEY=sk_test_123
OPENAI_API_KEY=sk-abc

Never Commit Secrets

Never commit secrets: add .env to .gitignore and ship a .env.example with empty values, so teammates know what to fill in without seeing real keys.

# .gitignore
.env
.env.local

Public vs Private Variables

In Next.js, only variables prefixed NEXT_PUBLIC_ reach the browser bundle — everything else stays server-only. Never prefix a real secret, or every visitor sees it.

NEXT_PUBLIC_APP_URL=https://app.com   # safe in browser
STRIPE_SECRET_KEY=sk_live_xxx          # server only

Reading Variables

Read variables through process.env. Server code can access any of them; client code only ever sees the public, NEXT_PUBLIC_ ones.

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
const url = process.env.NEXT_PUBLIC_APP_URL;

Validating Env at Startup

A missing key should fail fast, not at 2am in production. Validate required variables on boot with a schema like Zod so problems surface immediately.

import { z } from 'zod';
const env = z.object({
  DATABASE_URL: z.string().url(),
  STRIPE_SECRET_KEY: z.string().min(1)
}).parse(process.env);

Per-Environment Files

Next.js loads .env.local, then .env.development or .env.production. These per-environment files keep different values cleanly separated.

Secrets in Hosting Platforms

In production you don't ship a .env file. Set variables in your host's dashboard — Vercel, Render — so they're injected securely at runtime.

vercel env add STRIPE_SECRET_KEY production

Rotating Keys

If a key leaks, rotate it: generate a new one, update your env store, redeploy. Because keys live outside code, rotation is quick and low-risk.

Avoiding Common Leaks

Watch for common leaks: logging full process.env, returning secrets in API responses, hardcoding keys as fallbacks, or committing a real .env.

Best Practices

Best practices: keep secrets in .env and out of git, expose only truly public values with NEXT_PUBLIC_, validate at startup, and store prod secrets in your host.

Quick Check

Test your secrets knowledge.

Recap

Recap: store config in .env and keep it out of git, use NEXT_PUBLIC_ only for safe values, validate at startup, and set prod secrets in your host.

Domande Frequenti

La lezione «Variabili d’ambiente e gestione dei secrets» è gratuita?

Sì — il testo completo di «Variabili d’ambiente e gestione dei secrets» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso AI Powered SaaS: Stripe + Auth + Billing + Deploy, passa a CoddyKit PRO. Il corso AI Powered SaaS: Stripe + Auth + Billing + Deploy include 4 lezioni in totale.

Cosa imparerò in «Variabili d’ambiente e gestione dei secrets»?

Configuri in sicurezza il Suo SaaS nei diversi ambienti gestendo i secrets, separando le variabili pubbliche da quelle private ed evitando di esporre le chiavi al client. Eserciti AI Powered SaaS: Stripe + Auth + Billing + Deploy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare AI Powered SaaS: Stripe + Auth + Billing + Deploy?

Non è richiesta alcuna esperienza precedente. AI Powered SaaS: Stripe + Auth + Billing + Deploy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Variabili d’ambiente e gestione dei secrets»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione AI Powered SaaS: Stripe + Auth + Billing + Deploy?

Sì. Ogni lezione AI Powered SaaS: Stripe + Auth + Billing + Deploy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Introduzione alla sinergia tra SaaS e IA
  2. Scelta dello stack tecnologico
  3. Inizializzazione e struttura del progetto
  4. Variabili d’ambiente e gestione dei secrets
← Torna a AI Powered SaaS: Stripe + Auth + Billing + Deploy