Skip to main content

Overview

Sorionlib includes a powerful API client for making HTTP requests with built-in authentication, retries, and error handling.
import { ApiClient } from "sorionlib";

const api = new ApiClient({
  baseUrl: "https://api.example.com"
});

Authentication

API key authentication

const api = new ApiClient({
  baseUrl: "https://api.example.com",
  apiKey: "your-api-key"
});

Bearer token authentication

const api = new ApiClient({
  baseUrl: "https://api.example.com",
  token: "your-bearer-token"
});

Custom headers

const api = new ApiClient({
  baseUrl: "https://api.example.com",
  headers: {
    "X-Custom-Header": "value"
  }
});

Available methods

get(endpoint, params)

Makes a GET request.
ParameterTypeDescription
endpointstringThe API endpoint.
paramsobjectQuery parameters (optional).
Returns: Promise<object> - The response data.
const users = await api.get("/users", { limit: 10 });

post(endpoint, data)

Makes a POST request.
ParameterTypeDescription
endpointstringThe API endpoint.
dataobjectRequest body.
Returns: Promise<object> - The response data.
const user = await api.post("/users", {
  name: "John",
  email: "john@example.com"
});

put(endpoint, data)

Makes a PUT request.
ParameterTypeDescription
endpointstringThe API endpoint.
dataobjectRequest body.
Returns: Promise<object> - The response data.
const updated = await api.put("/users/123", {
  name: "John Updated"
});

delete(endpoint)

Makes a DELETE request.
ParameterTypeDescription
endpointstringThe API endpoint.
Returns: Promise<object> - The response data.
await api.delete("/users/123");

Error handling

The API client throws typed errors that you can catch and handle:
import { ApiClient, ApiError } from "sorionlib";

const api = new ApiClient({
  baseUrl: "https://api.example.com"
});

try {
  const data = await api.get("/users");
} catch (error) {
  if (error instanceof ApiError) {
    console.log(error.status);  // HTTP status code
    console.log(error.message); // Error message
    console.log(error.code);    // Error code
  }
}

Common error codes

CodeDescription
NETWORK_ERRORNetwork connection failed.
TIMEOUTRequest timed out.
UNAUTHORIZEDAuthentication failed.
FORBIDDENAccess denied.
NOT_FOUNDResource not found.
RATE_LIMITEDToo many requests.
SERVER_ERRORServer error occurred.

Configuration options

OptionTypeDefaultDescription
baseUrlstring-Base URL for all requests.
apiKeystring-API key for authentication.
tokenstring-Bearer token for authentication.
headersobject{}Custom headers.
timeoutnumber30000Request timeout in ms.
retriesnumber3Number of retry attempts.