Is it possible to build a REST API in a Canister?

I would like to build a backend with REST API is this possible? I only found examples for Web apps. Thanks.

Yes. You can implement http_request in your canister and service requests that way.

Currently only query calls that don’t mutate state are natively supported (not update).

I made a pull request to change that but was asked to wait for a proper proposal process to be in place.

See for details.

1 Like

Worth noting that in the meantime you could run my fork of icx-proxy somewhere, use @nomeata’s HTTP-to-IC bridge, or your own solution.

Hopefully something supporting update calls will land and remove the need for all that.

Also worth nothing that I think I prefer @nomeata’s approach of allowing the canister to request that the proxy “upgrade” the call from query to

“upgrade” means to make the call again as an update call, so in general it has the potential to always make 2 calls instead of 1, but it makes no assumptions about how HTTP methods map to the IC.

The compelling examples against my approach from the linked discussion include the need to use HTTP POST but not update state, e.g. an image transcoding service.

In that case my approach is inefficient because it makes an update call when a query would do.

The real problem with my approach is that if you need to support an existing API client which makes something like a GET request but then the canister needs to make a state modification. @nomeata’s approach handles that just fine, although there the inefficiency is in making 2 calls.

1 Like

Thanks for providing the options.

What’s the advantage of building out a separate REST API versus just using the existing Candid RPC platform?

I can think of these:

  1. Providing a backend that matches an existing API without replacing clients
  2. An interim solution until more agents are written in languages that clients support