# Idiomatic OCaml REST backend?

**URL:** <https://discuss.ocaml.org/t/idiomatic-ocaml-rest-backend/11693>\
**Category:** Learning\
**Created:** [March 17, 2023, 9:17pm UTC](https://discuss.ocaml.org/t/idiomatic-ocaml-rest-backend/11693 "2023-03-17T21:17:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [March 17, 2023, 9:17pm UTC](https://discuss.ocaml.org/t/idiomatic-ocaml-rest-backend/11693/1 "2023-03-17T21:17:20Z")

</div>

What is a good tutorial for an idiomatic OCaml REST backend? I’m just looking for something with good defaults instead of reinventing everything myself.

So far the closest I have found is [Joe Thomas | Writing a REST API with Dream](https://jsthomas.github.io/ocaml-dream-api.html)

Suggestions ?

---

<div class="post-metadata">

**Author:** ![zbaylin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zbaylin/32/4047_2.png) [@zbaylin](https://discuss.ocaml.org/u/zbaylin)\
**Post date:** [March 17, 2023, 9:39pm UTC](https://discuss.ocaml.org/t/idiomatic-ocaml-rest-backend/11693/2 "2023-03-17T21:39:04Z")

</div>

I don’t think there exists an “idiomatic” way of implementing REST web servers in OCaml – the closest thing is probably Dream, which you found. There’s also @rgrinberg’s [opium](https://github.com/rgrinberg/opium), which is more analogous to Ruby’s Sinatra.

If you’re looking for examples to work off of in particular, [dream has a whole host of them](https://github.com/aantron/dream/tree/master/example), [as does opium](https://github.com/rgrinberg/opium/tree/master/example).

---

<div class="post-metadata">

**Author:** ![R\_Huxton](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/r_huxton/32/4216_2.png) [@R\_Huxton](https://discuss.ocaml.org/u/R_Huxton)\
**Post date:** [March 17, 2023, 10:37pm UTC](https://discuss.ocaml.org/t/idiomatic-ocaml-rest-backend/11693/3 "2023-03-17T22:37:39Z")

</div>

Dream does seem to have all the basic elements you would expect put together in a clear and straightforward way. The documentation and examples can get you started quickly.

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [March 18, 2023, 12:20am UTC](https://discuss.ocaml.org/t/idiomatic-ocaml-rest-backend/11693/4 "2023-03-18T00:20:50Z")

</div>

Depends on what you mean by ‘REST’. If you mean the common understanding of ‘HTTP API with request and response bodies encoded as JSON objects’, then Dream can do the job pretty well. If you mean something closer to Roy Fielding’s hypermedia stuff, technically HTML is the closest you can get to that, and a backend that renders HTML responses is a REST API 🙂

---

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [March 18, 2023, 12:51am UTC](https://discuss.ocaml.org/t/idiomatic-ocaml-rest-backend/11693/5 "2023-03-18T00:51:52Z")

</div>

> [@yawaramin](#):
>
> Depends on what you mean by ‘REST’.

Upon reflection, I think “Ruby on Rails Replacement” might have been clearer.

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [March 18, 2023, 1:24am UTC](https://discuss.ocaml.org/t/idiomatic-ocaml-rest-backend/11693/6 "2023-03-18T01:24:17Z")

</div>

I think the closest we have to that is [Sihl](https://github.com/oxidizing/sihl).

---

<div class="post-metadata">

**Author:** ![cemerick](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/cemerick/32/1383_2.png) [@cemerick](https://discuss.ocaml.org/u/cemerick)\
**Post date:** [March 18, 2023, 2:11am UTC](https://discuss.ocaml.org/t/idiomatic-ocaml-rest-backend/11693/7 "2023-03-18T02:11:40Z")

</div>

I haven’t used it myself, but if one is interested in doing “real” REST, then [GitHub - inhabitedtype/ocaml-webmachine: A REST toolkit for OCaml](https://github.com/inhabitedtype/ocaml-webmachine) might be worth looking at. I don’t know how complete/faithful to the classic Erlang-based [webmachine](https://github.com/Webmachine/webmachine) implementation; but if it’s close, then it should be a great tool to consider.

---

<div class="post-metadata">

**Author:** ![rbjorklin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/rbjorklin/32/1126_2.png) [@rbjorklin](https://discuss.ocaml.org/u/rbjorklin)\
**Post date:** [March 18, 2023, 6:18am UTC](https://discuss.ocaml.org/t/idiomatic-ocaml-rest-backend/11693/8 "2023-03-18T06:18:49Z")

</div>

I added a simple webmachine implementation to the techempower benchmark and it’s basically Python level performance so I don’t think that’s a good choice 😅

> **[TechEmpower Web Framework Performance Comparison](https://www.techempower.com/benchmarks/#section=data-r21&test=json&l=zik0zj-67z)**
>
> Performance comparison of a wide spectrum of web application frameworks and platforms using community-contributed test implementations.

---

<div class="post-metadata">

**Author:** ![cemerick](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/cemerick/32/1383_2.png) [@cemerick](https://discuss.ocaml.org/u/cemerick)\
**Post date:** [March 18, 2023, 4:38pm UTC](https://discuss.ocaml.org/t/idiomatic-ocaml-rest-backend/11693/9 "2023-03-18T16:38:33Z")

</div>

> [@rbjorklin](#):
>
> I added a simple webmachine implementation to the techempower benchmark and it’s basically Python level performance so I don’t think that’s a good choice 😅

I don’t know anything about those benchmarks, but that webmachine result outranks django’s (for example), which is of course _widely_ used. I like performance like anyone else, but it’s not the only factor. Webmachine-esque approaches are uniquely appealing _if_ one is interested in being precise in one’s implementation of the fullest breadth of HTTP compliance, so it’s never going to be as fast as an approach that just mounts a pile of routes and handlers of interest (my usual approach, for example).
