How to securely hide an API key when calling a third-party HTTP API from a browser SPA?
Short answer
C Use Amazon API Gateway to create a REST API. Create an HTTP integration to integrate with the third-party HTTP API. Add the company's API key to the HTTP headers list of the integration request configuration.
Question
Key Takeaways
This question tests whether candidates know that API Gateway HTTP integrations can inject static headers server-side, eliminating the need for a Lambda function and keeping costs low while protecting secrets from the browser.
To hide a third-party API key from a public single-page application, use Amazon API Gateway REST API with an HTTP integration that injects the key in the integration request headers. This is the most cost-effective approach because it avoids the compute cost of an AWS Lambda proxy.
Common mistake: Many candidates choose option D (Lambda proxy integration) because it is a familiar pattern for calling external APIs, but it adds unnecessary Lambda invocation cost and complexity compared to a direct HTTP integration.
Expert Analysis
The scenario describes a public SPA that must call a third-party HTTP API using a secret API key. Because the browser is untrusted, the key cannot be embedded in the client code or sent directly from the browser. A server-side component must inject the key before the request leaves AWS.
Option C is correct. A public Amazon API Gateway REST API with an HTTP integration can forward the browser's request directly to the third-party endpoint. In the integration request mapping template, the company's API key is added as a static HTTP header. The key is stored only in the API Gateway configuration, never reaching the client. Because there is no Lambda function, you pay only for API Gateway requests, making this the most cost-effective solution.
Option A is incorrect because a private REST API requires a VPC endpoint (AWS PrivateLink) to be reachable, which adds significant cost and is unnecessary for a public-facing SPA.
Option B is incorrect for the same reason as A (private API + VPC endpoint) and also adds Lambda cost.
Option D works functionally but is not the most cost-effective. A Lambda proxy integration introduces a Lambda invocation for every request, increasing both cost and latency compared to the native HTTP integration mapping available in API Gateway.
Community members correctly note that the private API options (A and B) imply a VPC endpoint, which is not cost-effective, and that Lambda adds unnecessary overhead compared to API Gateway's built-in header mapping.
Official Reference
- https://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-http-integrations.html
- https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-rest-api.html
- https://stackoverflow.com/questions/62443262/aws-api-gateway-to-external-rest-api
These links open the official vendor documentation in a new tab.
Exam Strategy
When a question asks for the MOST cost-effective serverless solution, always compare native AWS integrations (such as API Gateway HTTP integrations) against Lambda-based solutions first. Native integrations usually win on cost and latency unless custom logic is required.
Community Votes
66% of anonymous learners picked answer C. Votes are pick records left by other test-takers — they are not the verified answer.
Discussion 7 contributions
Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.
From public community threads 7
-
wh1t4k3r 👍 3 Answered CIm going with C as per this post: https://stackoverflow.com/questions/62443262/aws-api-gateway-to-external-rest-api I am discarding the use of a private rest api because that would imply a VPC endpoint required and that is not cost effective.
-
YUICH 👍 3C. Amazon API Gateway with REST API and HTTP integration is the best solution because it allows the company to securely add the API key to requests sent to the third-party HTTP API without exposing the API key to the client.
-
stevesuperdx 👍 3C: While D would work, but it introduces extra Lambda overhead, increasing both complexity and cost compared to directly using API Gateway’s HTTP integration.
-
CloudChingon 👍 1 Answered CCorrect Answer: C Why Option C Works: Secure: The API key is added server-side in the API Gateway integration, ensuring it is not exposed to users. Cost-Effective: Avoids the cost of running a Lambda function while still using API Gateway for request routing. Public Access: The REST API is publicly accessible to the web client.
-
tullio85 👍 1 Answered ABy using a private REST API with an HTTP integration and adding the API key to the HTTP headers list, you can securely forward requests to the third-party API without exposing the API key to the client, while also minimizing costs by avoiding the need for a Lambda function for each request.
Show 2 more comments
-
jasonczx 👍 1 Answered Dhttps://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
-
aragon_saa 👍 1 Answered DAnswer is D
Reader comments & corrections
No reader corrections yet — spotted an error or have a note? Share it below.
Related Analysis
Practice All DVA-C02 Questions
Access 100 questions with complete answers and detailed explanations.
View Full DVA-C02 Practice Test →