Welcome! New to Spiderz? Get 40% off your first purchase.

Handle API keys, environment variables and databases

How-to 6 min ·

Get your project's settings right for the live site: which values are safe in the browser, which must stay secret on a server, and how to connect a database without losing data. Use this when your AI or your hosting instructions mention API keys or environment variables.

An environment variable is a named setting your app reads when it runs or builds, such as API_URL or DATABASE_PASSWORD. Projects usually keep them in a file called .env.

1 List every setting, without the secret values

Ask your AI to list the project's settings: the name, what it's for, whether it's public or secret, and where it's used. You'll end up with something like:

Setting What it's for Where it goes
Website address Links and return addresses Public settings
API address The backend the frontend calls Frontend build
Publishable key Browser access a provider allows Frontend build, if the provider says so
Database password Signing in to the database Server only
Secret API key Full access to a service Server only
Email password Sending email Server only

The list explains the values; it shouldn't contain them.

2 Keep secrets out of the browser

Anything sent to the browser can be read by anyone who visits, even if the page never shows it. That includes values built into your JavaScript.

In Vite, any variable starting with VITE_ ends up in the browser. Never give a password or secret key that prefix. Vite's environment variables explains how this works; other frameworks have their own equivalent.

Some providers issue publishable keys that are meant to be public. Supabase, for example, has publishable and secret keys, and its access rules protect the data either way. Supabase's API keys Follow each provider's guidance: "key" doesn't always mean secret, or always mean safe.

If a feature needs a secret key, that work belongs on a server. Don't move the key into the frontend to make an error go away.

3 Know when each setting is read

When your app reads a setting decides what you do after changing it:

Read After a change
When the frontend is built Build again and upload
When the backend starts Update its settings and restart it
From a framework's settings cache Clear the cache the framework's way
In a service's own dashboard Save it there and test

An API address built into the frontend needs a new build. A database password read by the backend only needs updating on the server.

4 Switch to the live values

Go through each setting and replace development values with live ones:

  • local addresses such as localhost with the public https:// address;
  • the test database with the live one;
  • test email settings with your real sending service;
  • sign-in return addresses and webhook addresses with your domain;
  • test payment keys with live ones, only once you've tested payments.

Keep each service consistent: don't mix a live key with a test webhook.

Keep a .env.example file in the project with every name and placeholder values, so the next person knows what's needed. Never upload the real .env to a public folder or commit it to a repository. If it sits in public_html, anyone can download it.

5 Decide where the database runs

Before entering a database address, know who hosts the database and how the app reaches it.

  • A database in your hosting account. Create it in cPanel under MySQL Databases, with its own user and password. Your backend connects to it; check that the app supports MySQL.
  • An outside database service. Note who owns the account, who pays, which addresses can connect, and whether your domain needs adding.

Never put a database username and password in frontend code to connect from the browser. If the project uses a browser-facing data service, follow that service's sign-in and access rules.

6 Set up the tables without losing data

Connecting to a database doesn't create its tables. Ask your AI how the structure is set up: usually with migrations, scripts that create and change tables, or an import file.

Before running any database command, check:

  • which database it affects;
  • whether it creates, changes or deletes tables or records;
  • whether it adds sample data;
  • that you have a backup you can restore;
  • that the app's code matches the new structure.

Keep database changes separate from uploading files. Putting old files back doesn't undo a database change. Back up the database and any uploaded files, even if they're hosted elsewhere.

7 Test who can see what

A working connection only proves the app reached something. Check that it can do the right things, and no more.

With test accounts you control, check that each person can do what they should. If the app keeps private records, make sure one test user can't see another's by changing an address or ID. For email, check the message arrives from the right address. For file storage, check who can upload, view and delete.

If the browser console mentions CORS, the API hasn't been told to accept requests from your domain. CORS controls which sites a browser lets read the API's responses; it doesn't replace sign-in or database permissions. MDN explains CORS

If a secret was exposed

Replace it, or revoke it, in the provider's dashboard straight away. Deleting it from the code doesn't help if it was already downloaded, committed or published.

Check the provider's activity log if it has one, put the new value in your server settings and rebuild anything that used the old one. Don't paste the exposed value anywhere else, even to ask for help.

Ask your AI for a settings review

Review this project's live settings without printing any secret values. For each setting, give its name, purpose, whether it's public or secret, where it's used, and whether it's read at build time or when the app runs. Check the files to be uploaded for any secrets. List local or preview addresses that must change, database setup still to do, and outside services not yet configured. Don't reset databases, change keys or touch live services. Give me a short plan and the tests to run afterwards.

Keep the result with your hosting handover, so the next update starts from a clear record.

Did this solve it?

Reference docs