通过名字获取性别 – 单次请求
通过名字获取性别接口允许你判断单个人名的性别。 所有参数通过 POST 请求以 JSON 格式发送。你必须在 Authorization 头中使用 Bearer token 方法进行身份验证。
请求 URL
POST https://api.genderapi.io/api
必需的 HTTP 头
Content-Type: application/json
Authorization: Bearer YOUR_API_KEY
请求体参数
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
name | String | 是 | 你要分析的名字。必须是单个词,不能包含头衔或前缀。 |
country | String | 否 |
两位国家代码(ISO 3166-1 alpha-2),提升预测准确率。
例如土耳其用TR 。
|
askToAI | Boolean | 否 |
如果设为 true ,当名字无法在数据库中找到时,API 会请求 AI 模型判断性别。
|
forceToGenderize | Boolean | 否 |
如果设为 true ,即使输入内容不是常见人名(如昵称或虚构名称,例如sparkling unicorn ),API 也会尝试判断性别。
|
请求示例
cURL 示例
curl -X POST "https://api.genderapi.io/api" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{"name": "Alice", "country": "US", "askToAI": true, "forceToGenderize": true}'
PHP cURL 示例
<?php
$url = "https://api.genderapi.io/api";
$data = array(
"name" => "Alice",
"country" => "US",
"askToAI" => true,
"forceToGenderize" => true
);
$payload = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Authorization": "Bearer YOUR_API_KEY"
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>
JavaScript fetch 示例
fetch("https://api.genderapi.io/api", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_API_KEY"
},
body: JSON.stringify({
name: "Alice",
country: "US",
askToAI: true,
forceToGenderize: true
})
})
.then(response => response.json())
.then(data => console.log(data));
Python requests 示例
import requests
url = "https://api.genderapi.io/api"
payload = {
"name": "Alice",
"country": "US",
"askToAI": True,
"forceToGenderize": True
}
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_API_KEY"
}
response = requests.post(url, headers=headers, json=payload)
print(response.json())
JSON 响应示例
{
"status": true,
"used_credits": 1,
"remaining_credits": 4999,
"expires": 1743659200,
"q": "Alice",
"name": "Alice",
"gender": "female",
"country": "US",
"total_names": 10234,
"probability": 98,
"duration": "4ms"
}
响应字段
字段 | 类型 | 说明 |
---|---|---|
status | Boolean | 请求是否成功。 |
used_credits | Integer | 本次请求消耗的积分。 |
remaining_credits | Integer | 此请求后账户剩余的积分。 |
expires | Integer (timestamp) | 套餐到期时间,UNIX 时间戳。 |
q | String | 你的输入查询(所提交的名字)。 |
name | String | 分析或提取出的名字。 |
gender | Enum[String] | 预测的性别。可能值:male 、female 或 null 。 |
country | String | 预测时考虑的国家代码。 |
total_names | Integer | 用于本次预测的样本数量。 |
probability | Integer | 性别预测的置信度百分比。 |
duration | String | 请求处理耗时(如4ms )。 |
注意:
请确保输入的名字等字段不包含意外字符。
如果你计划使用特殊字符或空格,请确保正确使用 JSON 编码,以避免出错。