Response Format
Format Standar Response
Semua response API mengikuti format standar yang konsisten untuk memudahkan penanganan di sisi client.
Struktur Dasar:
{
"status": "success|error",
"code": 200|201|400|etc,
"message": "Pesan response",
"data": null|{},
"timestamp": 1234567890
}
Komponen Response
Field | Tipe | Deskripsi |
---|---|---|
status | string | Status response ("success" atau "error") |
code | integer | HTTP status code |
message | string | Pesan deskriptif tentang hasil operasi |
data | mixed | Data utama response (object, array, atau null) |
timestamp | integer | Unix timestamp saat response dibuat |
Contoh Response
Successful Response
{
"status": "success",
"code": 200,
"message": "Data berhasil diambil",
"data": {
"id": 123,
"name": "Example",
"created_at": "2024-03-21T10:00:00Z"
},
"timestamp": 1710766800
}
Error Response
{
"status": "error",
"code": 400,
"message": "Validasi gagal",
"data": {
"errors": {
"name": "Field name harus diisi",
"email": "Format email tidak valid"
}
},
"timestamp": 1710766800
}
Collection Response
{
"status": "success",
"code": 200,
"message": "Data berhasil diambil",
"data": {
"items": [
{
"id": 1,
"name": "Item 1"
},
{
"id": 2,
"name": "Item 2"
}
],
"pagination": {
"total": 100,
"per_page": 10,
"current_page": 1,
"last_page": 10
}
},
"timestamp": 1710766800
}
Best Practices
- Selalu gunakan format yang konsisten untuk semua endpoint
- Sertakan informasi yang cukup dalam pesan error
- Gunakan HTTP status code yang sesuai
- Sertakan pagination info untuk collection data
- Timestamp menggunakan format Unix timestamp