Browse guides

Send requests with Valyrian.js

Load a JSON profile and render its loading, success, and error states.

The self-contained example requests a data URL, awaits the parsed profile and refreshes the mounted view with the result or rejected error.

Table of contents

Main APIs

request
Provides the asynchronous Valyrian.js HTTP client entry point.
ts
import { mount, update } from "valyrian.js";
import { request } from "valyrian.js/request";

type Profile = { name: string; role: string };
const profileUrl = `data:application/json,${encodeURIComponent(
  JSON.stringify({ name: "Arya", role: "editor" }),
)}`;

let status = "loading";
let result = "Loading profile";

mount("body", () => `${status}: ${result}`);

try {
  const profile = (await request.get(profileUrl)) as Profile;
  status = "success";
  result = `${profile.name}: ${profile.role}`;
} catch (error) {
  status = "error";
  result = error instanceof Error ? error.message : "Profile request failed";
} finally {
  update();
}

Error

Rejected requests and HTTP 4xx or 5xx responses enter the error path of the calling flow.

Apply this concept in Taskboard

API reference