Sign up and contact support to get 100 free checks.Sign up free
AvatarLookup workflow illustration for Add Avatar Image Analysis to a Web App Without Exposing the API Key
A visual overview of the workflow discussed in this AvatarLookup article.

Keep the analysis key on your backend and authorize each image submission before forwarding it to AvatarLookup.

To protect an API key during a frontend image upload, send the picture to your authenticated application backend. The backend validates the file, checks permission and forwards it to AvatarLookup using its server-held key.

Why a frontend environment variable does not hide the key

A contact-management app adds an “Analyze picture” button beside a saved image. It is tempting to call the analysis API directly from that button. The request works, but the person using the browser can inspect the credential that paid for it.

A build-time environment variable is a way to configure a build. If the build substitutes its value into browser JavaScript, the value becomes part of the downloaded application. Minification changes how easy it is to read, not who receives it. Source maps, request headers and client-visible exceptions provide other routes to the same disclosure.

Keep the AvatarLookup key in a server-side secret store or server environment. Never return it in configuration JSON or place it in a URL. CORS controls aspects of browser access to responses; it does not make a credential already delivered to a browser secret. OWASP's frontend security guidance likewise puts security-sensitive decisions on the server.

Give the browser and backend different responsibilities

The browser should select a picture, show a preview and report upload progress. It authenticates to your application with your existing session mechanism. Your backend decides whether that user may analyze that picture and spend the application's analysis allowance.

Step Browser Your backend
Choose image File picker and preview Receive bounded upload
Validate Friendly early feedback Authoritative byte and format checks
Authorize Present application session Check user, tenant and permitted action
Call analysis Wait for application response Add X-API-Key and submit multipart file
Show result Render approved fields Parse result and return a restricted response

An application route such as POST /app/image-analysis is yours to implement; it is not an AvatarLookup endpoint. Prefer an upload-only route for this feature. A client should not be able to select an arbitrary upstream URL, change the product or pass additional provider headers through the relay.

Validate the file, then build a new multipart request

Our image profile endpoint accepts JPEG, PNG, WebP or GIF, up to 2 MB. Check the received byte count and actual image format on the server. A .jpg filename or browser-supplied MIME type alone is insufficient. Use a maintained image parser with resource limits, and reject unreadable or unsupported input before making the paid request.

The following is application pseudocode. Authentication, file inspection and quota functions represent controls you implement in your backend, rather than functions supplied by AvatarLookup.

handle POST /app/image-analysis:
  user = require_authenticated_application_user()
  require_permission(user, "analyze-contact-picture")
  enforce_application_request_and_spend_limits(user)
  image = receive_bounded_file(field="file", max_size="2 MB")
  verify_decodable_format(image, JPEG | PNG | WebP | GIF)
  form = new_multipart_body()
  form.add("service_type", "image_profile")
  form.add_file("file", image)
  response = POST https://api.avatarlookup.com/api/v1/image/profile
             headers: X-API-Key = server_secret
             body: form
  return approved_result_or_application_error(response)

Let the multipart library generate the boundary and corresponding content type. Send the actual file in file; the website's public-URL input is not a reason to substitute an undocumented image_url API body. If you re-encode an image locally, validate the final bytes that will be submitted.

Handle the response as analysis of that picture. Appearance attributes are estimates, and a completed result can include unknown attributes or a nonhuman category. Your UI can display those outcomes without turning them into identity claims.

Keep the backend from becoming an anonymous paid relay

Hiding the provider key solves credential disclosure, but an unrestricted application endpoint can still spend your balance. Require authentication before accepting work, then enforce authorization for the particular account or contact record. A valid session in one tenant should not authorize analysis for another tenant's records.

Apply request and spending controls per user and tenant, with a shared account ceiling for concurrent callers. Reserve your own allowance atomically before forwarding work, so simultaneous requests cannot all pass the same remaining-budget check. These are application controls; their names and limits are your choices.

For cookie-authenticated state-changing requests, use your framework's CSRF protection. Set upload and upstream timeouts. Disable repeat submission while a request is pending, and do not silently resubmit an ambiguous timeout: your application may not know whether the original request completed.

Log an application request ID, outcome and duration. Redact authorization headers and avoid recording image bytes or entire upstream request objects. Return a short client-safe error with the request ID; keep restricted operational details on the server.

Inspect the built application before release

Inspect the production build, not just the source directory. Search distributed JavaScript, source maps and generated configuration for secret values or accidental secret substitutions. Review which environment-variable prefixes your build exposes to client code.

Next, upload a permitted test picture while watching the browser network panel. The browser should contact your application endpoint without an AvatarLookup credential in headers, query strings or response bodies. Force a validation error and an upstream failure to inspect error responses too.

Test an unauthenticated request, a request for another tenant's record and an exhausted application allowance. Confirm each stops before the provider call. Check the resulting logs for secret and image leakage. If a real key was exposed, remove the exposure and replace the compromised credential through the available account controls before restoring access.

Use the image profile API documentation to confirm the final multipart request, then make this release inspection part of every change to the upload flow.

Learn More

Choose the product information that fits the next step in your workflow.

Sources