Extraits prêts à l'emploi pour les opérations les plus courantes. Adaptez la base URL au module (voir Environnements, domaines et ports).
Se connecter et appeler (cURL)¶
# 1) obtenir un JWT
JWT=$(curl -s -X POST https://identity.puwapi.com/auth/login \
-H "Content-Type: application/json" \
-d '{"email":"me@ex.com","password":"•••"}' \
| python3 -c 'import sys,json;print(json.load(sys.stdin)["accessToken"])')
# 2) appeler une API de module
curl https://work.puwapi.com/api/work/items -H "Authorization: Bearer $JWT"
Appel avec clé API serveur (cURL)¶
curl https://knowledge.api.puwapi.com/api/public/knowledge/spaces \
-H "X-Server-API-Key: pkk_live_xxx"
Python (requests)¶
import requests
S = requests.Session()
tok = S.post("https://identity.puwapi.com/auth/login",
json={"email": "me@ex.com", "password": "•••"}).json()["accessToken"]
S.headers["Authorization"] = f"Bearer {tok}"
items = S.get("https://work.puwapi.com/api/work/items").json()
Python (clé API, sans dépendance)¶
import json, urllib.request
def call(method, url, key, body=None):
data = json.dumps(body).encode() if body else None
r = urllib.request.Request(url, data=data, method=method)
r.add_header("X-Server-API-Key", key)
if data: r.add_header("Content-Type", "application/json")
with urllib.request.urlopen(r) as resp:
return json.loads(resp.read() or "{}")
spaces = call("GET", "https://knowledge.api.puwapi.com/api/public/knowledge/spaces", KEY)
JavaScript / TypeScript (fetch)¶
async function api(path: string, jwt: string, init: RequestInit = {}) {
const res = await fetch(`https://work.puwapi.com${path}`, {
...init,
headers: { Authorization: `Bearer ${jwt}`, "Content-Type": "application/json", ...init.headers },
});
if (!res.ok) throw new Error(`${res.status} ${await res.text()}`);
return res.json();
}
const items = await api("/api/work/items", jwt);
Gérer le rafraîchissement du jeton (JS)¶
async function withRefresh(call: () => Promise<Response>, refresh: () => Promise<string>) {
let res = await call();
if (res.status === 401) { await refresh(); res = await call(); }
return res;
}
Bonnes manières¶
- Vérifiez toujours le code HTTP et lisez
{"error": …}en cas d'échec. - Réutilisez une session/un client HTTP (connexions persistantes).
- Pour l'asynchrone (IA, envoi d'e-mails), suivez le statut par l'
idrenvoyé plutôt que d'attendre une réponse synchrone.