Code-Beispiele
cURL-Beispiele
Anmeldeanfrage
curl -X POST https://landlordkeeper.com/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{"email": "[email protected]", "password": "YourPassword123"}'
Profil abrufen
curl -X GET https://landlordkeeper.com/api/v1/user/profile \
-H "Authorization: Bearer YOUR_JWT_TOKEN"
Profil aktualisieren
curl -X PUT https://landlordkeeper.com/api/v1/user/profile \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-d '{"name": "New Name", "phone": "+380501234567"}'
JavaScript-Beispiele
Anmeldefunktion
// Login and get token
async function login(email, password) {
const response = await fetch('/api/v1/auth/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ email, password }),
});
if (!response.ok) {
throw new Error('Login failed');
}
const data = await response.json();
localStorage.setItem('token', data.token);
return data.token;
}
Helper für authentifizierte Anfragen
// Make authenticated request
async function apiRequest(endpoint, options = {}) {
const token = localStorage.getItem('token');
const response = await fetch(`/api${endpoint}`, {
...options,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
...options.headers,
},
});
if (response.status === 401) {
// Token expired - redirect to login
localStorage.removeItem('token');
window.location.href = '/login';
return;
}
return response.json();
}
// Usage
const profile = await apiRequest('/user/profile');
console.log(profile);
PHP-Beispiele
Verwendung von Guzzle HTTP Client
<?php
use GuzzleHttp\Client;
$client = new Client(['base_uri' => 'https://landlordkeeper.com']);
// Login
$response = $client->post('/api/v1/auth/login', [
'json' => [
'email' => '[email protected]',
'password' => 'YourPassword123',
],
]);
$data = json_decode($response->getBody(), true);
$token = $data['token'];
// Get profile
$response = $client->get('/api/v1/user/profile', [
'headers' => [
'Authorization' => 'Bearer ' . $token,
],
]);
$profile = json_decode($response->getBody(), true);
print_r($profile);
Python-Beispiele
Verwendung der requests-Bibliothek
import requests
BASE_URL = "https://landlordkeeper.com"
# Login
response = requests.post(f"{BASE_URL}/api/v1/auth/login", json={
"email": "[email protected]",
"password": "YourPassword123"
})
token = response.json()["token"]
# Get profile
headers = {"Authorization": f"Bearer {token}"}
response = requests.get(f"{BASE_URL}/api/v1/user/profile", headers=headers)
profile = response.json()
print(profile)
# Update profile
response = requests.put(
f"{BASE_URL}/api/v1/user/profile",
headers=headers,
json={"name": "New Name"}
)
print(response.json())
Typischer Arbeitsablauf
Hier ist ein typischer Arbeitsablauf zur Verwendung der API:
1
Registrieren oder anmelden
Erstellen Sie ein neues Konto oder melden Sie sich mit vorhandenen Daten an, um ein JWT-Token zu erhalten.
2
Token speichern
Speichern Sie das JWT-Token an einem sicheren Ort in Ihrer Anwendung (localStorage, geschütztes Cookie usw.).
3
API-Anfragen ausführen
Fügen Sie das Token im Authorization-Header für alle geschützten Endpoints ein.
4
Token-Ablauf behandeln
Bei einem 401-Fehler leiten Sie den Benutzer zur Anmeldeseite um, um ein neues Token zu erhalten.