Home › Library › REST API › Code Examples

Code Examples

cURL Examples

Login request

curl -X POST https://landlordkeeper.com/api/v1/auth/login \
  -H "Content-Type: application/json" \
  -d '{"email": "[email protected]", "password": "YourPassword123"}'

Get profile

curl -X GET https://landlordkeeper.com/api/v1/user/profile \
  -H "Authorization: Bearer YOUR_JWT_TOKEN"

Update profile

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 Examples

Login function

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

Authenticated request helper

// 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 Examples

Using 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 Examples

Using requests library

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

Typical Workflow

Here is a typical workflow for using the API:

1
Register or Login

Create a new account or authenticate with existing credentials to get a JWT token.

2
Store the Token

Store the JWT token securely in your application (localStorage, secure cookie, etc.).

3
Make API Requests

Include the token in Authorization header for all protected endpoints.

4
Handle Token Expiry

When you receive a 401 error, redirect user to login to get a new token.