Skip to main content

Full Application Flow & Results

This tutorial builds a complete backend-style script in Kyna: configure options, read files, make an asynchronous HTTP request, parse JSON, and handle failures.


Complete Application Code​

class ServiceClient {
private baseUrl: str;
private apiKey: str;

public init(baseUrl: str, apiKey: str) {
self.baseUrl = baseUrl;
self.apiKey = apiKey;
}

public async fn checkHealth(serviceName: str): str {
const start = timeNow();
const url = self.baseUrl + "/health/" + serviceName;

try {
const res = await fetch(url, {
method: "GET",
headers: {
"Authorization": "Bearer " + self.apiKey,
"Accept": "application/json"
},
timeout: 5000
});

const elapsedMs = (timeNow() - start) / 1000000;

if (res.ok) {
const data = res.json();
return "[OK " + elapsedMs + "ms] " + serviceName + " -> Status: " + data["status"];
} else {
return "[FAIL " + res.status + "] " + serviceName;
}
} catch (err) {
return "[ERR " + err.code + "] " + serviceName + ": " + err.message;
}
}
}

async fn main() {
slogInfo("initializing");
const client = new ServiceClient("https://api.kyna.dev", "kyna_live_key_992");

const services = ["auth", "database", "analytics"];
loop (var i = 0; i < len(services); i = i + 1) {
const result = await client.checkHealth(services[i]);
slogInfo(result);
}
}

await main();

Expected Output​

JSON lines on stdout, for example:

{"level":"info","msg":"initializing"}
{"level":"info","msg":"[OK 42ms] auth -> Status: healthy"}