Layer LogoWAVS Docs
WAVS builders handbookComponents

Network requests

Components can make network requests to external APIs using the wavs-wasi-utils crate.

To learn how to use variables like API keys in a component, visit the Variables page.

Dependencies

The following dependencies are required for making HTTP requests from a component:

Cargo.toml
[dependencies]
wavs-wasi-utils = "0.4.0" # HTTP utilities
wstd = "0.5.3" # Runtime utilities (includes block_on)
serde = { version = "1.0.219", features = ["derive"] } # Serialization
serde_json = "1.0.140" # JSON handling

Since WASI components run in a synchronous environment but network requests are asynchronous, you can use block_on from the wstd crate to bridge this gap. The block_on function allows you to run async code within a synchronous context, which is essential for making HTTP requests in WAVS components.

Making HTTP requests

The wavs-wasi-utils crate provides several functions for making HTTP requests. See the HTTP module documentation for more details.

// Request functions
http_request_get(url) // Creates a GET request
http_request_post_json(url, data) // Creates a POST request with JSON data
http_request_post_form(url, data) // Creates a POST request with form data
// Response functions
fetch_json(request) // Fetches and parses JSON response
fetch_string(request) // Fetches response as string
fetch_bytes(request) // Fetches raw response bytes

Example: GET request with headers

Here's an example showing how to make a GET request with custom headers:

lib.rs
use wstd::runtime::block_on;
use wstd::http::HeaderValue;
use wavs_wasi_utils::http::{fetch_json, http_request_get};
use serde::{Deserialize, Serialize};
// Define response type with serde derive for automatic JSON parsing
#[derive(Debug, Serialize, Deserialize)]
struct ApiResponse {
// ... your response fields
}
async fn make_request() -> Result<ApiResponse, String> {
let url = "https://api.example.com/endpoint";
let mut req = http_request_get(&url).map_err(|e| e.to_string())?;
// Set required headers for API requests
req.headers_mut().insert(
"Accept",
HeaderValue::from_static("application/json")
);
req.headers_mut().insert(
"Content-Type",
HeaderValue::from_static("application/json")
);
req.headers_mut().insert(
"User-Agent",
HeaderValue::from_static("Mozilla/5.0")
);
// Use fetch_json to automatically parse the response
let json: ApiResponse = fetch_json(req)
.await
.map_err(|e| e.to_string())?;
Ok(json)
}
// Use block_on to handle async code in sync context
fn process_data() -> Result<ApiResponse, String> {
block_on(async move {
make_request().await
})?
}

Example: POST request with JSON data

For making POST requests with JSON data, you can use the http_request_post_json helper function:

lib.rs
use wstd::runtime::block_on;
use wavs_wasi_utils::http::{fetch_json, http_request_post_json};
use serde::{Deserialize, Serialize};
// Define request and response types with serde derive
#[derive(Debug, Serialize, Deserialize)]
struct PostData {
key1: String,
key2: i32,
}
#[derive(Debug, Serialize, Deserialize)]
struct PostResponse {
// ... response fields
}
async fn make_post_request() -> Result<PostResponse, String> {
let url = "https://api.example.com/endpoint";
let post_data = PostData {
key1: "value1".to_string(),
key2: 42,
};
// http_request_post_json automatically sets JSON headers
let response: PostResponse = fetch_json(
http_request_post_json(&url, &post_data)?
).await.map_err(|e| e.to_string())?;
Ok(response)
}
fn process_data() -> Result<PostResponse, String> {
block_on(async move {
make_post_request().await
})?
}

For more details, visit the wavs-wasi-utils documentation.

Edit on GitHub

On this page