Making web calls to OCaml

Hi, I am new to OCaml and in fact, I’m not a even a programmer (although I did study CS at Cornell back in the 80’s and learned functional programming in a language called scheme.) I am thinking about developing financial wellness web applications with the underlying computations in OCaml, but with the user interface in something else - like java script. How would a java script website make a call to an Ocaml program (or function)? Or put another way, can I publish a financial model built in OCaml so that a web (or mobile) application could call it - passing arguments to the function and receiving back the result of evaluating the functional expression? My apologies if this is not asked correctly or if it is a very basic question, but I am not sure I have the right terminology to ask the question properly! Help is appreciated!

You can, there are multiple ways to achieve what you want. If I understand correctly you want to build a web server in OCaml and a web app front end. Here’s a list of what different projects used:

Edit: To elaborate because I realized I didn’t answer your question, typically you’d have a frontend which uses something like rest or graphql to fetch data from your server. There’s a lot to unpack here. I’m sure you’ll be able to pull it off but if you’re not comfortable with programming make sure that you approach the problem gradually and make sure to avoid analysis paralysis.


Thank you Wokalski! I appreciate the fast response and the advice. I will read up on rest and graphql and hopefully that will point me in the right direction…and I might have some follow-up questions at some point too.

Hi, a couple of thoughts here. As @wokalski said, you will need to set up a server application, and a web frontend. I don’t know much about your background but, my guess is you would like to avoid complexity and keep things simple. Personally here’s what I would recommend:

  1. Write a simple command-line application, in the style of a Unix filter, in OCaml that takes ‘requests’ in the form of plain text on standard input and prints its calculation result to standard output. E.g., to take an input of add 2 2 and output 4, it could work like this:
$ echo 'add 2 2' | my_calculator.exe
  1. Next, use websocketd to wrap your calculator tool and serve it over WebSocket, which is a standard Web technology that allows clients to continuously talk to servers (2-way communication). So, clients could send a plain text command like add 2 2 (note, exactly the same as you would have on the command line), and get back a response 4.

  2. Finally, write a web application (just some HTML and JavaScript) that connects to the WebSocket server from step (2) and sends and receives messages. Here is an example of that:

The reason I am recommending this strategy, is to let you start small and simple, and skip over much of the complexity of dealing with modern web application development. You can focus on writing your calculator as a simple command-line tool, and ‘outsource’ the server part to a specialized tool.

Final thought: if you are working on a financial wellness tool, you almost certainly need decimal arithmetic (as opposed to binary arithmetic from OCaml’s built-in float type). You will want to use a decimal package like , or (disclaimer: mine) .

Good luck!


@peter I think I’m doing something similar – a simple web tool for geographic calculations from character sequences called geohash to gps coordinate pairs and vice versa. Here is it: You’ll find the source there, too.

Key is, I scale towards n=1, need no state.

The backend is <200 LOC to handle all the http stuff (there isn’t much, no auth, no state, no cookies) and another ~100 LOC for the actual computation.

1 dependency, no ‘modern’ (i.e. immature and volatile) web toolkit, no client libs/frameworks, no concurrency

P.S.: and it’s an API, too: $ curl