Skip to content
TheChatArchiveThe Chat Archive
chatgpt-4o-latest-20250326· OpenAIHugging Face LMArena Human Preference 140K

What are the best practice approaches to have an web service that uses some end user…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.5-flash-lite-preview-06-17-thinking’s answer.
  1. User

    What are the best practice approaches to have an web service that uses some end user secret api keys in its logic? E.g. llm api keys provided by user? Im thinking about storing the keys only on the client side (e.g. local storage) and just send them to the backend actions that require it, so not storing anything on server side

  2. Assistant

    Great question — dealing with user-provided secrets like API keys (e.g. for LLMs) involves critical security and architectural considerations. Let's break down best practice approaches for handling such secrets in a web service, especially when they're user-owned (bring-your-own-api-key style).


    💡 Typical Use Case:

    Your app allows users to plug in their own API keys (e.g. OpenAI API keys) to enable features via your backend, but you don’t want to store these secrets long-term server-side.


    ✅ Best Practice Approaches

    1. Don't Persist Secrets Server Side Unless Explicitly Required

    You're right that not storing user secrets on your backend is the safest default. Instead:

    • Store in Memory Temporarily only for the duration of the request.
    • Do not log or persist the key.
    • Avoid caching any API calls that include user-provided secrets.

    2. Client-Side Storage with Caution (localStorage, sessionStorage)

    If you're storing API keys on the client:

    • Prefer sessionStorage over localStorage (wipes on tab close, not persisted long-term).
    • Never store secrets in cookies, especially not without HttpOnly and Secure.

    Make sure your documentation makes clear to users that keys are stored locally and are their responsibility to protect.


    3. End-to-End API Key Usage Flow (Stateless Server)

    Here’s a pattern that matches your goals:

    1. User enters API key in the frontend.
    2. Frontend stores it in sessionStorage.
    3. When calling your backend for a service:
      • The key is included as part of the request body or an Authorization header.
    4. Backend uses the key once, for the user’s API request to OpenAI (or similar).
    5. Backend discards it immediately.

    This is a stateless pattern and avoids long-term key handling risk.


    4. Encrypt In Transit – Always Use HTTPS

    • Ensure your application forces HTTPS.
    • Protect all data in transit, especially sensitive payloads like API keys.

    5. AuthZ/AuthN Your Users Even if You Don't Store Their Secrets

    • Use authentication (OAuth, password login, etc.) for your own application.
    • You might still want to map user sessions to the API requests they make via their keys — for rate limiting or abuse prevention.

    6. Optional: Vault Storage (If You Later Want to Store the Keys)

    If at some point you'd like to store the keys (e.g. to offer background jobs), make sure to:

    • Encrypt keys using a secure encryption scheme (AES-256).
    • Use a Key Management System (KMS) like:
      • AWS KMS
      • HashiCorp Vault
      • GCP Secret Manager
    • Ideally, encrypt per-user, and require opt-in consent.

    7. Sanitization, Logging & Exploits

    • Scrub keys from all logs, errors, and telemetry systems.
    • Don't output user-supplied API keys in frontend errors (visible in DevTools).
    • Sentry/log tools should be reviewed to ensure PII/API key redaction.

    8. Rate Limiting & Abuse Protection

    Even if you're not storing keys, protect your backend from:

    • Malicious users spamming keys and pushing costs to OpenAI.
    • Use IP-based or token-based rate limiting per session/client.

    If using a stateless flow — a malicious user could flood your backend with requests using different stolen keys. So some form of user authentication / session management / API usage quotas is advisable to mitigate this.


    9. Allow Key Testing (Optional UX Feature)

    Provide a "Test API key" option in your UI to verify upfront if the key works, to avoid failing mid-task.

    You can do this client-side or server-side in a safe way (e.g. test /v1/models endpoint or a similar harmless call).


    10. Use Strict CORS Rules

    If your frontend and backend are separate, ensure only trusted origins can send requests to your backend:

    Access-Control-Allow-Origin: https://your-app.com
    

    Wildcards here are dangerous (*) if credentials are sent.


    🔐 Summary

    Best PracticeDescription
    Stateless server-sideUse keys only per-request, don’t store
    Client-side short-term storageStore in sessionStorage, not cookies
    Use HTTPSAlways
    Avoid key loggingSanitize logs and exceptions
    Rate limit and abuse protectPrevent cost drain or misuse
    Optional: Vault/KMSFor encrypted storage, if needed later
    Validate key up-frontHelp user UX with quick validation
    Auth for your own systemEven if you don’t use stored keys

    Want an example code flow (e.g. for React + Express or Next.js)? Just ask!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.