Exemple de implementare
Exemple practice pentru integrarea API-ului SIRUTA în aplicația ta. Copiază codul și adaptează-l la nevoile tale.
Dropdown-uri Județ + Localitate
Selectează un județ și localitățile se încarcă automat. Cel mai comun caz de utilizare.
Demo live
JavaScript
// Alpine.js component
function dropdownExample() {
return {
counties: [],
localities: [],
selectedCounty: '',
selectedLocality: '',
postalCode: '',
async init() {
// Încarcă județele la start
const res = await fetch('https://api.siruta.ro/v1/counties', {
headers: { 'X-Site-Token': 'YOUR_TOKEN' }
});
const data = await res.json();
this.counties = data.data;
},
async loadLocalities() {
if (!this.selectedCounty) return;
const res = await fetch(
`https://api.siruta.ro/v1/counties/${this.selectedCounty}/localities/lite`,
{ headers: { 'X-Site-Token': 'YOUR_TOKEN' } }
);
const data = await res.json();
this.localities = data.data;
this.selectedLocality = '';
this.postalCode = '';
},
setPostalCode() {
const loc = this.localities.find(l => l.siruta_code == this.selectedLocality);
this.postalCode = loc?.postal_code || '';
}
}
}
Căutare cu Autocomplete
Caută localități în timp real pe măsură ce utilizatorul tastează.
Demo live
Ai selectat:
Cod poștal:
JavaScript
function searchExample() {
return {
query: '',
results: [],
selected: null,
loading: false,
showResults: false,
async search() {
if (this.query.length < 2) {
this.results = [];
return;
}
this.loading = true;
const res = await fetch(
`https://api.siruta.ro/v1/localities?search=${this.query}`,
{ headers: { 'X-Site-Token': 'YOUR_TOKEN' } }
);
const data = await res.json();
this.results = data.data.slice(0, 10);
this.loading = false;
},
selectResult(result) {
this.selected = result;
this.query = result.name;
this.showResults = false;
}
}
}
Laravel / PHP
Integrare server-side cu Laravel HTTP Client sau cURL.
PHP - Laravel Service
// app/Services/SirutaService.php
namespace App\Services;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Cache;
class SirutaService
{
private string $baseUrl = 'https://api.siruta.ro/v1';
private string $token;
public function __construct()
{
$this->token = config('services.siruta.token');
}
public function getCounties(): array
{
return Cache::remember('siruta_counties', 86400, function () {
$response = Http::withHeaders([
'X-Site-Token' => $this->token,
])->get("{$this->baseUrl}/counties");
return $response->json('data');
});
}
public function getLocalities(string $countyAbbr): array
{
return Cache::remember("siruta_localities_{$countyAbbr}", 3600, function () use ($countyAbbr) {
$response = Http::withHeaders([
'X-Site-Token' => $this->token,
])->get("{$this->baseUrl}/counties/{$countyAbbr}/localities");
return $response->json('data');
});
}
}
React Hook
Custom hook pentru React cu loading state și error handling.
React - useSiruta Hook
// hooks/useSiruta.js
import { useState, useEffect } from 'react';
const API_URL = 'https://api.siruta.ro/v1';
const TOKEN = 'YOUR_TOKEN';
export function useSiruta() {
const [counties, setCounties] = useState([]);
const [localities, setLocalities] = useState([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
const headers = { 'X-Site-Token': TOKEN };
useEffect(() => {
fetch(`${API_URL}/counties`, { headers })
.then(res => res.json())
.then(data => setCounties(data.data))
.catch(err => setError(err.message));
}, []);
const loadLocalities = async (countyAbbr) => {
setLoading(true);
try {
const res = await fetch(
`${API_URL}/counties/${countyAbbr}/localities`,
{ headers }
);
const data = await res.json();
setLocalities(data.data);
} catch (err) {
setError(err.message);
} finally {
setLoading(false);
}
};
return { counties, localities, loading, error, loadLocalities };
}