Militant API wiki

Premier appel API

Pages annexes, exemples et details techniques dans une mise en page plus lisible.

Guide pour faire votre premier appel à l'API Militant.

1. Vérifier que l'API fonctionne

curl https://votre-instance.com/api/v1/

Vous devriez voir la liste des endpoints en JSON.

2. S'authentifier

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.

3. Faire un appel authentifié

Obtenir le fil d'actualité

curl https://votre-instance.com/api/v1/posts.php \
  -H "Authorization: Bearer VOTRE_TOKEN"

Créer un post

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 !"
  }'

Obtenir son profil

curl https://votre-instance.com/api/v1/users.php?username=votre_username \
  -H "Authorization: Bearer VOTRE_TOKEN"

4. Gérer les erreurs

Token invalide (401)

{
  "success": false,
  "error": "Invalid or expired token"
}

Solution : Refaire un login pour obtenir un nouveau token.

Rate limit dépassé (429)

{
  "success": false,
  "error": "Rate limit exceeded"
}

Solution : Attendre 1 heure ou réduire le nombre de requêtes.

Données invalides (400)

{
  "success": false,
  "error": "Invalid data"
}

Solution : Vérifier les paramètres envoyés.

5. Exemples avec différents langages

Python

import requests

Login

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

JavaScript

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

PHP

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

Prochaines étapes