Authentication
All API requests to GenderAPI require authentication. You can authenticate your requests in two ways:
- API Key in POST body as
key
parameter - API Key in Authorization header using a Bearer token
1. API Key in POST Body
The simplest way to authenticate is by including your API key as a field called key
in the JSON body of your POST request.
For example, to determine the gender of the name "Alice":
cURL Example
curl -X POST "https://api.genderapi.io/api" \
-H "Content-Type: application/json" \
-d '{"name": "Alice", "key": "YOUR_API_KEY"}'
JavaScript fetch Example
fetch("https://api.genderapi.io/api", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
name: "Alice",
key: "YOUR_API_KEY"
})
})
.then(response => response.json())
.then(data => console.log(data));
Python requests Example
import requests
url = "https://api.genderapi.io/api"
payload = {
"name": "Alice",
"key": "YOUR_API_KEY"
}
headers = {
"Content-Type": "application/json"
}
response = requests.post(url, headers=headers, json=payload)
print(response.json())
2. API Key in Authorization Header (Bearer Token)
For better security and cleaner requests, you can send your API key in the Authorization header using the Bearer scheme. In this method, you do not include the API key in your JSON body.
Instead, only your input parameters (like name
) go in the POST body.
For example, to determine the gender of the name "Alice":
cURL Example
curl -X POST "https://api.genderapi.io/api" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{"name": "Alice"}'
JavaScript fetch Example
fetch("https://api.genderapi.io/api", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_API_KEY"
},
body: JSON.stringify({
name: "Alice"
})
})
.then(response => response.json())
.then(data => console.log(data));
Python requests Example
import requests
url = "https://api.genderapi.io/api"
payload = {
"name": "Alice"
}
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_API_KEY"
}
response = requests.post(url, headers=headers, json=payload)
print(response.json())
Choosing Between the Two Methods
Both authentication methods are supported on all API endpoints. However, using the Authorization header is more secure because it keeps your API key out of the request body and avoids accidentally logging it.
Regardless of the method, always set the Content-Type
header to
application/json
when making POST requests.