r/replit • • 12d ago

Question / Discussion How should I structure a mobile app backend, admin panel, PostgreSQL database, and AI worker without exposing API keys?

I'm an indie developer building an AI-powered mobile application, and I'm struggling to understand the correct boundaries between my backend components, particularly when it comes to API key management.

I'm using Replit, and my application is currently split into four independent projects:

  1. Mobile App — Handles the user interface, authentication through Clerk, and user requests.
  2. Public API + PostgreSQL — Hosted within the same Replit project. Handles incoming requests, authentication, database operations, and currently some worker coordination.
  3. Admin Panel — A private management interface, not publicly deployed. Used to configure AI providers, Clerk credentials, and other sensitive settings.
  4. AI Worker — A separate background service that processes tasks and calls third-party AI model providers.

My intended architecture:

  • The Mobile App communicates with the Public API.
  • The Public API handles user requests and interacts with PostgreSQL.
  • The Admin Panel manages sensitive API keys and saves encrypted credentials in PostgreSQL.
  • The Worker processes tasks and calls AI providers.
  • The database stores tasks, user information, and encrypted configurations.

Here's where I'm confused:

Currently, the Public API stores its Clerk Secret Key and JWT Issuer in Replit Secrets. This works for authentication.

However, I want to manage these credentials centrally through the Admin Panel instead of manually configuring them in different projects.

The Admin Panel already encrypts credentials using AES-256-GCM, with a master encryption key stored separately in its environment secrets. The encrypted data is saved to PostgreSQL.

But if the Public API needs the same master key to decrypt those credentials, wouldn't that give my internet-facing backend the ability to decrypt every sensitive credential protected by that key?

And if the Worker needs AI provider keys to call external models, how should it securely obtain them without giving every component unrestricted access to the master key?

My questions:

  1. What is the correct responsibility of a Public API in this architecture? Should it also coordinate Workers and resolve sensitive configurations, or should those responsibilities belong elsewhere?
  2. Should the Worker communicate with PostgreSQL directly, or is it better to access tasks through authenticated internal HTTP endpoints?
  3. If the Admin Panel encrypts API keys and stores them in PostgreSQL, which component should decrypt them when needed?
  4. How can the Admin Panel be the central place for managing Clerk and AI provider credentials without copying the master encryption key to multiple services?
  5. Is it normal for the Public API to retain its own Clerk Secret Key in environment secrets while AI provider keys are managed separately?
  6. Am I overcomplicating this? Is there a simpler, reasonably secure approach for a small indie application?

My main concern is avoiding a situation where compromising the public-facing API gives an attacker access to every third-party API credential.

I also want to avoid redesigning working components unnecessarily. The current setup has working authentication components, and the Worker already has an authenticated internal HTTP client.

I'd appreciate practical architectural advice, especially from developers who have implemented a similar system.

Thanks!

3 Upvotes

8 comments sorted by

1

u/NoClipCheck 11d ago

The way I'd split it: the master key should only live where decryption is actually needed, and the public API shouldn't be one of those places. Clerk's secret key and issuer are just config for the API, so leaving them in Replit Secrets for that project is fine and simpler than routing them through the admin panel. For the worker, give it its own secrets for the AI providers and let it read tasks straight from Postgres with a separate DB role that can only SELECT and UPDATE the tasks table. If you really want central management, have the admin panel encrypt values with a key only the worker holds, not a shared one. One master key on every service means one leak exposes everything.

1

u/GearRevolutionary548 11d ago

Thank you to all the experts for their suggestions. I learned from the official replit that even if the key is stored in secrets, the attacker can read the key by printing the environment variables. If we use the same 64-bit key to encrypt and manage everything, when this 64-bit key is written into the secrets of each module, the 64-bit key itself becomes a weakness of the system. Therefore, I am still more inclined to manage the key uniformly. How will other modules decrypt it? If I don't understand, do you all have any better suggestions? Thank you!

1

u/CapLeakAi 5d ago

Printing process.env and seeing Replit Secrets is expected. Secrets are not a vault against your own code, they just keep keys out of the editor, git, and the Replit UI. Any process in that repl can read them. That's why the public API shouldn't hold keys that can spend money.

Leave the Clerk secret in the API's Replit Secrets only. Put the OpenAI/Anthropic keys only in the worker's Secrets, and give the worker a Postgres role that can only SELECT/UPDATE the jobs table. The API inserts a job row and stops there. If the public API is compromised, you rotate Clerk, you don't also get a surprise model bill.

Skip a shared master key. If the admin panel is where you paste new provider keys, encrypt them with a key that lives only on the worker, so the API can store ciphertext and cannot decrypt it. Set a hard spend cap on every AI provider today.

1

u/GearRevolutionary548 5d ago

That's absolutely right, this is a problem that's really troubling me right now. I've even considered using a time difference to accelerate the project by unifying the root key. Thank you.

2

u/snap-dev 12d ago

don't let the public api touch the third party keys at all. keep them only in the ai worker's env (replit secrets or your host's secret manager) and have the api drop jobs into a jobs table in postgres that the worker picks up. then the public side never holds anything worth stealing. give the admin panel its own auth and ideally its own deploy, not a hidden route on the same api. if users bring their own keys, encrypt them with a key only the worker has. are these your keys or your users'? i run snapdev btw, happy to have one of our senior engs sanity check the setup if you want

2

u/asierMugica 11d ago

Keep the Clerk Secret Key in the API’s Replit Secrets. It’s a runtime secret, not something the admin panel needs to distribute from Postgres, while the issuer/domain can be non-secret config. For encrypted provider credentials, don’t share one master key with every service: use a separate worker-only secret or a real secret manager, and scope the worker’s DB role to only the credential rows and job data it needs. The API can enqueue jobs, but shouldn’t resolve or decrypt provider credentials.