The fetch Command


fetch <stringLike> [ as [ a | an ]( json | Object | html | response ) ] [<object literal> | 'with' <naked named arguments>]


The fetch command issues a fetch request to the given URL. The URL can either be a naked URL or a string literal.

By default the result will be processed as text, but you can have it processed as JSON, as HTML, or as a raw response object by adding the as json, as html or as response modifiers.

Additionally, you can use conversions directly on the response text.

This command saves the result into the it variable.

This command is asynchronous.

Timeouts & Cancelling

The fetch command supports both timeouts as well as request cancellation.

To add a timeout to a request syntactically, you can use add a timeout property using the with form:

<button _="on click fetch /example with timeout:300ms
                    put the result into my innerHTML">
  Get from /example!

To cancel a fetch request, send a fetch:abort event to the element that triggered the request:

    <button id="btn1"
            _="on click
                 add @disabled
                 fetch /example
                 put the result after me
                 remove @disabled">
        Get Response
    <button _="on click send fetch:abort to #btn1">
        Cancel The Request

Dynamic URLs with Template Literals

If you need to fetch from a dynamically-generated URL, just use a template literal string (the ones with the backticks) as the URL. For example:

set userId to my [@data-userId]
fetch `/users/${userId}/profile` as JSON


The fetch command features a few events that can be listened to (using hyperscript or javascript) to do things like configure the fetch options, update UI state, etc.

event description
hyperscript:beforeFetch (Deprecated, use fetch:beforeRequest) Fired before a fetch request, can be used to configure headers, etc.
fetch:beforeRequest Fired before a fetch request, can be used to configure headers, etc.
fetch:afterResponse Fired after a fetch request completes but before the response is processed, can be used to mutate the response.
fetch:afterRequest Fired after a fetch response has been processed.
fetch:error Fired when an error occurs.

Below are a two examples showing how to configure an X-AuthToken header using the fetch:beforeRequest event:

<body _="on fetch:beforeRequest(headers)
            set headers['X-AuthToken'] to getAuthToken()">
document.body.addEventListener('fetch:beforeRequest', (event) => {
    event.detail.headers['X-AuthToken'] = getAuthToken();


<button _="on click fetch /example
                    put it into my innerHTML">
  Get from /example!

<button _='on click fetch /test as json with method:"POST"
                    put `${its result}` into my innerHTML'>
  Post to /test!

<button _="on click fetch `${pageUrl}` as html
                    get the textContent of the <h1/> in it
                    call alert(result)">
  Get the title of the page!

<div _='on click fetch /number as Number with method:"POST"
                 put "${the result + 1}" into my innerHTML'>