Autenticazione

Tutte le richieste API verso GenderAPI richiedono autenticazione. Puoi autenticare le tue richieste in due modi:

  1. API Key nel corpo della POST come parametro key
  2. API Key nell'header Authorization usando un Bearer token

1. API Key nel corpo della POST

Il modo più semplice per autenticarti è includere la tua API Key come campo denominato key nel corpo JSON della tua richiesta POST.

Ad esempio, per determinare il genere del nome “Alice”:


Esempio con cURL

curl -X POST "https://api.genderapi.io/api" \
     -H "Content-Type: application/json" \
     -d '{"name": "Alice", "key": "YOUR_API_KEY"}'

Esempio con JavaScript fetch

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));

Esempio con Python requests

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 nell'header Authorization (Bearer Token)

Per maggiore sicurezza e richieste più pulite, puoi inviare la tua API Key nell'header Authorization utilizzando lo schema Bearer. Con questo metodo, non devi includere la chiave API nel corpo JSON.

Al suo posto, solo i tuoi parametri di input (come name) vengono inviati nel corpo della POST.

Ad esempio, per determinare il genere del nome “Alice”:


Esempio con cURL

curl -X POST "https://api.genderapi.io/api" \
     -H "Content-Type: application/json" \
     -H "Authorization: Bearer YOUR_API_KEY" \
     -d '{"name": "Alice"}'

Esempio con 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"
  })
})
.then(response => response.json())
.then(data => console.log(data));

Esempio con Python requests

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())

Scelta tra i due metodi

Entrambi i metodi di autenticazione sono supportati su tutti gli endpoint API. Tuttavia, usare l'header Authorization è più sicuro perché mantiene la tua API Key fuori dal corpo della richiesta ed evita che venga registrata per errore.

Indipendentemente dal metodo scelto, assicurati sempre di impostare l'header Content-Type su application/json quando effettui richieste POST.