Calling a Web API via WebAssembly

Learn how to call a Web API using WebAssembly.

The evolution of the web has been phenomenal, with much of its growth attributed to its open standards. Today, the web provides hundreds of APIs, which makes it easy for web developers to develop for audio, video, canvases, SVGs, USBs, batteries, and so on. The web is universal and omnipresent. It’s continuously experimented with and changed in order to make it desirable and easy for developers and companies to use, respectively. The web-sys crate provides access to almost all the APIs that are available on the web at the moment.

The web-sys crate provides raw bindings to all the Web’s APIs: everything from DOM manipulation to WebGL to Web Audio to timers to fetch and more! We can find the raw bindings for all Web APIs at web-sys crates.io, located here. The WebIDL interface definitions are converted into wasm-bindgen’s internal abstract syntax trees (ASTs). Then, these ASTs are used to create zero-overhead Rust and JavaScript glue code. With the help of this binding code, we can call and manipulate the web APIs. Converting the web APIs into Rust ensures the type information of parameters and return values is handled correctly and safely.

Getting started with a project

In this example, let’s call a web API via WebAssembly:

  1. Create a default project with the cargo new command:

Get hands-on with 1200+ tech skills courses.