Pages annexes, exemples et details techniques dans une mise en page plus lisible.
Guide pour faire votre premier appel à l'API Militant.
curl https://votre-instance.com/api/v1/
Vous devriez voir la liste des endpoints en JSON.
curl -X POST https://votre-instance.com/api/v1/auth.php \
-H "Content-Type: application/json" \
-d '{
"username": "votre_username",
"password": "votre_password"
}'
Réponse :
{
"success": true,
"token": "abc123def456...",
"expires_at": "2026-03-04 12:00:00",
"user_id": 1
}
Copiez le token, vous en aurez besoin pour tous les appels suivants.
curl https://votre-instance.com/api/v1/posts.php \
-H "Authorization: Bearer VOTRE_TOKEN"
curl -X POST https://votre-instance.com/api/v1/posts.php \
-H "Authorization: Bearer VOTRE_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"content": "Mon premier post via l'\''API !"
}'
curl https://votre-instance.com/api/v1/users.php?username=votre_username \
-H "Authorization: Bearer VOTRE_TOKEN"
{
"success": false,
"error": "Invalid or expired token"
}
Solution : Refaire un login pour obtenir un nouveau token.
{
"success": false,
"error": "Rate limit exceeded"
}
Solution : Attendre 1 heure ou réduire le nombre de requêtes.
{
"success": false,
"error": "Invalid data"
}
Solution : Vérifier les paramètres envoyés.
import requestsLogin
response = requests.post('https://votre-instance.com/api/v1/auth.php', json={
'username': 'votre_username',
'password': 'votre_password'
})
token = response.json()['token']Get posts
response = requests.get('https://votre-instance.com/api/v1/posts.php',
headers={'Authorization': f'Bearer {token}'})
posts = response.json()
// Login
const response = await fetch('https://votre-instance.com/api/v1/auth.php', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
username: 'votre_username',
password: 'votre_password'
})
});
const {token} = await response.json();// Get posts
const postsResponse = await fetch('https://votre-instance.com/api/v1/posts.php', {
headers: {'Authorization': Bearer ${token}}
});
const posts = await postsResponse.json();
// Login
$ch = curl_init('https://votre-instance.com/api/v1/auth.php');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
'username' => 'votre_username',
'password' => 'votre_password'
]));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = json_decode(curl_exec($ch), true);
$token = $response['token'];// Get posts
$ch = curl_init('https://votre-instance.com/api/v1/posts.php');
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Bearer $token"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$posts = json_decode(curl_exec($ch), true);