Sprievodcovia
IČO API: JavaScript, Python a PHP
HTTP príklady vyhľadávania a detailu firmy podľa IČO v JavaScripte, Pythone a PHP vrátane ošetrenia chýb.
Vyskúšaj tento endpoint
{ "stav": "pripravený" }
Priame HTTP príklady bez vlastného SDK. JavaScript používa vstavaný fetch, Python knižnicu requests a PHP rozšírenie cURL. Príklady obsahujú ošetrenie chýb.
JavaScript / TypeScript — vyhľadávanie
async function searchCompanies(query: string, country = "sk") {
const params = new URLSearchParams({ q: query, country, limit: "10" });
const res = await fetch(`https://api.subjekt.sk/v1/search?${params}`);
if (!res.ok) {
const err = await res.json();
throw new Error(`API chyba ${res.status}: ${err.code} — ${err.message}`);
}
const data = await res.json();
return data.results;
}
// Použitie
try {
const firmy = await searchCompanies("slovnaft");
console.log(firmy);
} catch (e) {
console.error("Vyhľadávanie zlyhalo:", e);
}
JavaScript / TypeScript — detail entity
async function getEntity(ico: string, country = "sk") {
const res = await fetch(
`https://api.subjekt.sk/v1/entity/${ico}?country=${country}`
);
if (res.status === 404) {
return null; // firma nenájdená
}
if (!res.ok) {
const err = await res.json();
throw new Error(`API chyba ${res.status}: ${err.code} — ${err.message}`);
}
return res.json();
}
// Použitie
const entity = await getEntity("31322832");
if (entity) {
console.log(`${entity.name}, IČO: ${entity.ico}`);
} else {
console.log("Firma nenájdená");
}
Python — vyhľadávanie
import requests
def search_companies(query: str, country: str = "sk") -> list:
r = requests.get(
"https://api.subjekt.sk/v1/search",
params={"q": query, "country": country, "limit": 10},
timeout=8
)
r.raise_for_status() # vyhodí HTTPError pri 4xx/5xx
return r.json()["results"]
# Použitie
try:
firmy = search_companies("slovnaft")
for firma in firmy:
print(f"{firma['name']} ({firma['ico']})")
except requests.HTTPError as e:
print(f"API chyba: {e.response.status_code}")
except requests.RequestException as e:
print(f"Sieťová chyba: {e}")
Python — detail entity
import requests
def get_entity(ico: str, country: str = "sk") -> dict | None:
r = requests.get(
f"https://api.subjekt.sk/v1/entity/{ico}",
params={"country": country},
timeout=8
)
if r.status_code == 404:
return None # firma nenájdená
r.raise_for_status()
return r.json()
# Použitie
try:
entity = get_entity("31322832")
if entity:
print(f"{entity['name']}, DIČ: {entity.get('dic')}")
else:
print("Firma nenájdená")
except requests.HTTPError as e:
print(f"API chyba: {e.response.status_code}")
PHP — vyhľadávanie
<?php
function searchCompanies(string $query, string $country = "sk"): array {
$url = "https://api.subjekt.sk/v1/search?" . http_build_query([
"q" => $query,
"country" => $country,
"limit" => 10,
]);
$json = @file_get_contents($url);
if ($json === false) {
throw new RuntimeException("Sieťová chyba pri volaní API");
}
$data = json_decode($json, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException("Neplatná JSON odpoveď");
}
if (isset($data["code"])) {
throw new RuntimeException("API chyba: " . $data["code"] . " — " . $data["message"]);
}
return $data["results"] ?? [];
}
// Použitie
try {
$firmy = searchCompanies("slovnaft");
foreach ($firmy as $firma) {
echo $firma["name"] . " (" . $firma["ico"] . ")\n";
}
} catch (RuntimeException $e) {
echo "Chyba: " . $e->getMessage() . "\n";
}
PHP — detail entity
<?php
function getEntity(string $ico, string $country = "sk"): ?array {
$url = "https://api.subjekt.sk/v1/entity/{$ico}?" . http_build_query([
"country" => $country,
]);
$context = stream_context_create([
"http" => ["ignore_errors" => true],
]);
$json = file_get_contents($url, false, $context);
if ($json === false) {
throw new RuntimeException("Sieťová chyba pri volaní API");
}
$data = json_decode($json, true);
// Skontroluj HTTP status
$statusLine = $http_response_header[0] ?? "";
if (str_contains($statusLine, "404")) {
return null; // firma nenájdená
}
if (isset($data["code"])) {
throw new RuntimeException("API chyba: " . $data["code"] . " — " . $data["message"]);
}
return $data;
}
// Použitie
try {
$entity = getEntity("31322832");
if ($entity) {
echo $entity["name"] . ", IČO: " . $entity["ico"] . "\n";
} else {
echo "Firma nenájdená\n";
}
} catch (RuntimeException $e) {
echo "Chyba: " . $e->getMessage() . "\n";
}
Ďalšie príklady vrátane stránkovania nájdete na stránkach Stránkovanie a Bežné použitia.