Azle and Kybra now support the timers API

Azle 0.9.0 and Kybra 0.2.0 both have support for the new timers API.

If you haven’t heard of this API, it’s meant to be an improvement over heartbeat, allowing you to set callbacks at arbitrary times in the future. The hope is that this will be much cheaper for time-based tasks when compared with the coarse heartbeat API that forced an update call every second or so.

Azle timers info: GitHub - demergent-labs/azle: TypeScript CDK for the Internet Computer

import { Duration, ic, TimerId, Update } from 'azle';

export function set_timers(delay: Duration): Update<[TimerId, TimerId]> {
    const function_timer_id = ic.set_timer(delay, callback);

    const captured_value = '🚩';

    const closure_timer_id = ic.set_timer(delay, () => {
        console.log(`closure called and captured value ${captured_value}`);

    return [function_timer_id, closure_timer_id];

function callback(): void {
    console.log('callback called');

Kybra timers info: GitHub - demergent-labs/kybra: Python CDK for the Internet Computer

from kybra import Duration, ic, TimerId, update

def set_timers(delay: Duration) -> tuple[TimerId, TimerId]:
    function_timer_id = ic.set_timer(delay, callback)

    captured_value = "🚩"

    closure_timer_id = ic.set_timer(
        lambda: ic.print(f"closure called and captured value {captured_value}"),

    return [function_timer_id, closure_timer_id]

def callback():
    ic.print("callback called")

Do these two examples both run immediately once a canister starts, or do they need to be called and executed first?

These examples expose functions that would need to be called after deploying the canister, keep in mind that the Azle example is now very old and won’t work with the latest versions of Azle.

You can see the updated Azle example here: azle/examples/timers/src/timers.ts at main · demergent-labs/azle · GitHub