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.
query calls that don’t mutate state are natively supported (not
I made a pull request to change that but was asked to wait for a proper proposal process to be in place.
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
“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.
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:
- Providing a backend that matches an existing API without replacing clients
- An interim solution until more agents are written in languages that clients support