Instead of using a URLConnection, you should be using an HttpClient to make a request.
A simple example might look like this:
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(theUrl);
request.addHeader("x-api-key", apiKey);
HttpResponse response = httpclient.execute(request);
Answer from Bryan Herbst on Stack Overflow Top answer 1 of 2
18
Instead of using a URLConnection, you should be using an HttpClient to make a request.
A simple example might look like this:
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(theUrl);
request.addHeader("x-api-key", apiKey);
HttpResponse response = httpclient.execute(request);
2 of 2
7
The use of Apache HttpClient is now discouraged. You can check it here: Android 6.0 Changes
You should better use URLConnection and cast to HttpURLConnection:
HttpURLConnection huc= (HttpURLConnection) url.openConnection();
huc.setRequestProperty("x-api-key","value");
Apidog
apidog.com › blog › pass-x-api-key-header
How to Pass x-API-key in Header?
July 31, 2025 - To pass the x-API-key in the header using Apidog, follow these steps: Step 1: Launch Apidog and open the desired project. Step 2: Create a new API endpoint or select the desired endpoint at Apidog. Step 3: Within the API endpoint request section, navigate to Headers section. Step 4: On the header parameters, enter "x-API-key" as the name.
Videos
AWS
docs.aws.amazon.com › amazon api gateway › developer guide › api gateway rest apis › distribute your rest apis to clients in api gateway › usage plans and api keys for rest apis in api gateway › choose an api key source in api gateway
Choose an API key source in API Gateway - Amazon API Gateway
To have the client submit an API key, set the value to HEADER in the previous command. ... To choose an API key source for an API by using the API Gateway REST API, call restapi:update as follows: PATCH /restapis/fugvjdxtri/ HTTP/1.1 Content-Type: application/json Host: apigateway.us-east-1.amazonaws.com X-Amz-Date: 20160603T205348Z Authorization: AWS4-HMAC-SHA256 Credential={access_key_ID}/20160603/us-east-1/apigateway/aws4_request, SignedHeaders=content-length;content-type;host;x-amz-date, Signature={sig4_hash} { "patchOperations" : [ { "op" : "replace", "path" : "/apiKeySource", "value" : "HEADER" } ] }
Pirelli
developer.pirelli.com › docs › read › authorize_your_api_calls › XApiKey
Pirelli API Portal - X-Api-Key
Refer to the [Register your Application](/docs/read/Register_your_Application) section for instructions on how to get a key. ## General Use To call the resource endpoints put the key in the *X-Api-Key* HTTP header, like this:
Reddit
reddit.com › r/htmx › help: correct way of sending x-api-key in headers using htmx?
r/htmx on Reddit: Help: Correct way of sending X-API-KEY in headers using HTMX?
October 23, 2024 -
Hi everyone,
First of all I am thankful to all redditors who helped me on my previous posts. And, now, I am again stuck in a situation where I would like to send X-API-KEY in headers to my API.
I am sending my X-HTTP-KEY as following:
<form autocomplete="off" id="wa-link-form"
hx-post="<?= $baseUrl ?>/api/create_link"
hx-trigger="submit"
hx-target="#result"
hx-swap="outerHTML"
hx-headers='{"X-API-KEY": "<?php echo htmlspecialchars($apiKey); ?>"}'
hx-on::after-request="clearForm(); reissueCsrfToken();">So, I want to ask if this is the right way?
Top answer 1 of 5
27
Your front end shouldn't be doing anything with API keys. If an API key is encoded in your HTML, anyone with access to the website can copy it and use it for their own API calls, which invalidates the whole reason for the key in the first place. API keys should be server-to-server.
2 of 5
7
Keep the API key in your backend and make any necessary third-party API calls from there. If you need some kind of auth token for the backend to accept calls from the browser, use a standard auth strategy--make the user log in to your app and then set a secure, HTTP-only cookie. Also a couple more things: The form's submit event is htmx's default trigger. You don't need to specify hx-trigger=submit explicitly on a tag. I'd recommend giving the form the standard method and action attributes so that it would continue to work even if htmx or JavaScript were disabled. I am assuming that you have some named elements inside the form that would normally be submitted with it, for both htmx and standard form submissions.
Swagger
swagger.io › docs › specification › v3_0 › authentication › api-keys
API Keys | Swagger Docs
This example defines an API key named X-API-Key sent as a request header X-API-Key: <key>. The key name ApiKeyAuth is an arbitrary name for the security scheme (not to be confused with the API key name, which is specified by the name key). The name ApiKeyAuth is used again in the security section ...
Apigee
docs.apigee.com › api-platform › tutorials › secure-calls-your-api-through-api-key-validation
Secure an API by requiring API keys | Apigee Edge | Apigee Docs
curl -v -H "x-apikey: API_KEY" http://ORG_NAME-test.apigee.net/helloapikey · Note that to fully complete the change, you'd also need to configure the AssignMessage policy to remove the header instead of the query parameter. For example:
AWS
docs.aws.amazon.com › amazon api gateway › developer guide › openapi extensions for api gateway › x-amazon-apigateway-api-key-source property
x-amazon-apigateway-api-key-source property - Amazon API Gateway
{ "swagger" : "2.0", "info" : { "title" : "Test1" }, "schemes" : [ "https" ], "basePath" : "/import", "x-amazon-apigateway-api-key-source" : "HEADER", .
Postman
postman.com › postman › postman-blog › request › mvr1s8d › adding-a-request-header
Adding a request header | Securely using API keys in ...
We cannot provide a description for this page right now
Boomi Community
community.boomi.com › s › question › 0D56S00009LC5BVSA1 › xapikey-header-for-rest-get-requests
Question: X-API-KEY header for REST GET requests
Loading · ×Sorry to interrupt · Refresh
Laracasts
laracasts.com › discuss › channels › requests › laravel-restful-api-with-x-api-key-header
Laravel RESTful API with X-API-KEY header
We cannot provide a description for this page right now
Google
docs.cloud.google.com › application development › google cloud sdk › authentication › use api keys to access apis
Use API keys to access APIs | Authentication | Google Cloud Documentation
curl -X POST \ -H "X-goog-api-key: API_KEY" \ -H "Content-Type: application/json; charset=utf-8" \ -d @request.json \ "https://translation.googleapis.com/language/translate/v2" If you can't use the HTTP header, you can use the key query parameter. However, this method includes your API key in the URL, exposing your key to theft through URL scans. The following example shows how to use the key query parameter with a Cloud Natural Language API request for documents.analyzeEntities.
Google Cloud
cloud.google.com › application development › apigee › secure an api by requiring api keys
Secure an API by requiring API keys | Apigee | Google Cloud
curl -v -H "x-apikey: YOUR_API_KEY" http://YOUR_ENV_GROUP_HOSTNAME/helloapikey · Note that to fully complete the change, you'd also need to configure the Assign Message policy to remove the header instead of the query parameter. For example: