# Tutorial for Cohttp-lwt as API client

**URL:** <https://discuss.ocaml.org/t/tutorial-for-cohttp-lwt-as-api-client/2746>\
**Category:** Learning\
**Created:** [October 21, 2018, 4:11pm UTC](https://discuss.ocaml.org/t/tutorial-for-cohttp-lwt-as-api-client/2746 "2018-10-21T16:11:24Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![abhi18av](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/abhi18av/32/1262_2.png) [@abhi18av](https://discuss.ocaml.org/u/abhi18av)\
**Post date:** [October 21, 2018, 4:11pm UTC](https://discuss.ocaml.org/t/tutorial-for-cohttp-lwt-as-api-client/2746/1 "2018-10-21T16:11:24Z")

</div>

Hi everyone,

I was trying to get started with practical `OCaml` via writing API clients and decided to experiment with [https://deckofcardsapi.com/](https://deckofcardsapi.com/) as a starting point to discover the libraries.

However, the only pieces of documentation I’ve come across for these are the following

- [How to write a simple web client?](https://discuss.ocaml.org/t/how-to-write-a-simple-web-client/1756)
- The `client_lwt.ml` example in the `cohttp` github repo

But unfortunately, I haven’t quite been able to make my way through these. So, I decided to ask for help from the `OCaml` community regarding the same.

Could you guide me a bit here ?

---

<div class="post-metadata">

**Author:** ![mseri](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mseri/32/91_2.png) [@mseri](https://discuss.ocaml.org/u/mseri)\
**Post date:** [October 21, 2018, 4:59pm UTC](https://discuss.ocaml.org/t/tutorial-for-cohttp-lwt-as-api-client/2746/2 "2018-10-21T16:59:52Z")

</div>

There is also the [example in Real world ocaml](http://dev.realworldocaml.org/concurrent-programming.html#example-searching-definitions-with-duckduckgo), althouh that uses `cohttp-async`. It should be relatively straightforward to translate.

Having a nice tutorial to add to the repo and the community documentation would be good. I am not a power user, but I can try to help. What you would loke to know?

---

<div class="post-metadata">

**Author:** ![abhi18av](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/abhi18av/32/1262_2.png) [@abhi18av](https://discuss.ocaml.org/u/abhi18av)\
**Post date:** [October 22, 2018, 2:02pm UTC](https://discuss.ocaml.org/t/tutorial-for-cohttp-lwt-as-api-client/2746/3 "2018-10-22T14:02:06Z")

</div>

Thanks @mseri for the link!

However, I’d really love to get started with `lwt` as I’m more inclined towards the `mirageOS` that relies on `lwt` .

For now, what I’d like to do is to just wrap the deckofcards API in a utop session as the starting point. If possible, could you please share the snippets that could help me accomplish these tasks ?

Here are the libraries I’ve identified so far

- `yojson`
- `lwt`
- `cohttp-lwt`

Are there any good resources to learn `lwt` apart from the main `ocsigen` docs ?

---

<div class="post-metadata">

**Author:** ![abhi18av](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/abhi18av/32/1262_2.png) [@abhi18av](https://discuss.ocaml.org/u/abhi18av)\
**Post date:** [October 22, 2018, 2:03pm UTC](https://discuss.ocaml.org/t/tutorial-for-cohttp-lwt-as-api-client/2746/4 "2018-10-22T14:03:37Z")

</div>

I wish to use `OCaml` as my own standard way to wrap and join the various online APIs which I often find using in other language clients.

---

<div class="post-metadata">

**Author:** ![mseri](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mseri/32/91_2.png) [@mseri](https://discuss.ocaml.org/u/mseri)\
**Post date:** [October 22, 2018, 3:01pm UTC](https://discuss.ocaml.org/t/tutorial-for-cohttp-lwt-as-api-client/2746/5 "2018-10-22T15:01:09Z")

</div>

@abhi18av I hope the following will help, it’s just a rough example. You can actually copy and paste it in `utop`. You will need `cohttp-lwt-unix` and `yojson` installed. For `yojson` refer to [RWO](http://dev.realworldocaml.org/json.html#handling-json-data), it is fine even if you don’t use `core`.

```ocaml
#require "cohttp-lwt-unix";;
#require "yojson";;

(* A bit overkill to open everything *)
open Cohttp
open Cohttp_lwt_unix
open Lwt.Infix
open Yojson
;;

(* Make a get call and return the body parsed as json.
 * You could use ppx_deriving_yojson or atdgen to
 * reduce the json parsing boilerplate, but for such small
 * example is overkill.
 *
 * val json_body : string -> Basic.json Lwt.t
 *)
let json_body uri =
  Client.get (Uri.of_string uri) >>= fun (_resp, body) ->
  (* here you could check the headers or the response status
   * and deal with errors, see the example on cohttp repo README *)
  body |> Cohttp_lwt.Body.to_string >|= Yojson.Basic.from_string
;;

(* In utop the promise is realised immediately, so if you run the
 * function above, you'll get the result immediately *)
json_body "http://deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1"
;;
(*Output:
- : Basic.json =
`Assoc
  [("shuffled", `Bool true); ("success", `Bool true); ("remaining", `Int 52);
   ("deck_id", `String "2svxenasd9qj")]
*)

(* In the code you will have more likely something like the following. *)
let shuffled_deck =
  json_body "http://deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1"
;;
(* val shuffled_deck : Basic.json Lwt.t *)

(* To use it you just have to get use to the monadic bind, and once you
 * are ready, call `Lwt_main.run`. e.g. the kind of useless example below *)

(* val get_cards : Basic.json -> int -> Basic.json Lwt.t *)
let get_cards sdeck n =
  (* The use of Yojson is well explained in RWO *)
  let module YBU = Yojson.Basic.Util in
  let success = sdeck |> YBU.member "success" |> YBU.to_bool in
  if not success then Lwt.fail_with "Error while shuffling" else
    let deck_id = sdeck |> YBU.member "deck_id" |> YBU.to_string in
    Printf.sprintf "http://deckofcardsapi.com/api/deck/%s/draw/?count=%d" deck_id n
    |> json_body >>= fun cards ->
    Lwt.return cards

(* val do_something : unit -> string list Lwt.t *)
let do_something () =
  json_body "http://deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1" >>= fun sdeck ->
  get_cards sdeck 3 >>= fun jcards ->
  let module YBU = Yojson.Basic.Util in
  let cards = jcards |> YBU.member "cards" |> YBU.to_list in
  List.map (fun c -> c |> YBU.member "code" |> YBU.to_string) cards
  |> Lwt.return

(* val do_something_else : unit -> unit Lwt.t *)
let do_something_else () =
  do_something () >>= fun cards ->
  List.iter (Printf.printf "%s\n") cards |> Lwt.return;;
;;

Lwt_main.run (do_something_else ()); print_endline "Done!"
;;

(* Be careful with the signatures, the example above can be re-run multiple
 * times, while the one below does nothing after the first run *)

(* val do_something_1 : string list Lwt.t *)
let do_something_1 =
  json_body "http://deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1" >>= fun sdeck ->
  get_cards sdeck 3 >>= fun jcards ->
  let module YBU = Yojson.Basic.Util in
  let cards = jcards |> YBU.member "cards" |> YBU.to_list in
  List.map (fun c -> c |> YBU.member "code" |> YBU.to_string) cards
  |> Lwt.return

(* val do_something_else_1 : unit Lwt.t *)
let do_something_else_1 =
  do_something_1 >>= fun cards ->
  List.iter (Printf.printf "%s\n") cards |> Lwt.return;;
;;

Lwt_main.run do_something_else_1; print_endline "Done_1!"
;;
Lwt_main.run do_something_else_1; print_endline "Done_1!"
;;
Lwt_main.run do_something_else_1; print_endline "Done_1!"
;;
Lwt_main.run (do_something_else ()); print_endline "Done!"
;;

```

---

<div class="post-metadata">

**Author:** ![emillon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/emillon/32/1206_2.png) [@emillon](https://discuss.ocaml.org/u/emillon)\
**Post date:** [October 22, 2018, 4:38pm UTC](https://discuss.ocaml.org/t/tutorial-for-cohttp-lwt-as-api-client/2746/7 "2018-10-22T16:38:08Z")

</div>

Hi,

I recently published [zeit](https://github.com/emillon/ocaml-zeit) which is client library for a JSON REST API using cohttp-lwt, yojson and ppx\_deriving\_yojson. You might be interested by [the Client module](https://github.com/emillon/ocaml-zeit/blob/cdcdd0b155d406d1b8c8947e3c620527c3c9ecf7/lib/client.ml).

---

<div class="post-metadata">

**Author:** ![mjambon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mjambon/32/6180_2.png) [@mjambon](https://discuss.ocaml.org/u/mjambon)\
**Post date:** [October 22, 2018, 5:29pm UTC](https://discuss.ocaml.org/t/tutorial-for-cohttp-lwt-as-api-client/2746/8 "2018-10-22T17:29:58Z")

</div>

Managing json APIs is what [atdgen](https://atd.readthedocs.io/en/latest/) was designed for. You write your types and `atdgen` derives the OCaml code to convert your data from/to json safely. It also provides ways to deal with APIs that don’t fit the OCaml type system perfectly (json adapters being the most flexible but last-resort method).

---

<div class="post-metadata">

**Author:** ![mjambon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mjambon/32/6180_2.png) [@mjambon](https://discuss.ocaml.org/u/mjambon)\
**Post date:** [October 22, 2018, 5:43pm UTC](https://discuss.ocaml.org/t/tutorial-for-cohttp-lwt-as-api-client/2746/9 "2018-10-22T17:43:10Z")

</div>

These are the type definitions that we used at my previous company for using the Google Calendar API: [https://github.com/esperco/esper-gcal-api](https://github.com/esperco/esper-gcal-api)

---

<div class="post-metadata">

**Author:** ![abhi18av](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/abhi18av/32/1262_2.png) [@abhi18av](https://discuss.ocaml.org/u/abhi18av)\
**Post date:** [October 31, 2018, 1:35am UTC](https://discuss.ocaml.org/t/tutorial-for-cohttp-lwt-as-api-client/2746/10 "2018-10-31T01:35:49Z")

</div>

Thanks for the reverts everone!

I have since started looking into `atdgen` as a way to use typed-json interaction also `zeit` has been of help for sure 🙂

---

<div class="post-metadata">

**Author:** ![abhi18av](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/abhi18av/32/1262_2.png) [@abhi18av](https://discuss.ocaml.org/u/abhi18av)\
**Post date:** [October 31, 2018, 1:37am UTC](https://discuss.ocaml.org/t/tutorial-for-cohttp-lwt-as-api-client/2746/11 "2018-10-31T01:37:06Z")

</div>

While trying out this example, I did come across the following error

```auto

 (* val do_something : unit -> string list Lwt.t *)
let do_something () =
  json_body "http://deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1" >>= fun sdeck ->
  get_cards sdeck 3 >>= fun jcards ->
  let module YBU = Yojson.Basic.Util in
  let cards = jcards |> YBU.member "cards" |> YBU.to_list in
  List.map (fun c -> c |> YBU.member "code" |> YBU.to_string) cards
  |> Lwt.return

;;
Error: This expression should not be a function, the expected type is
'a list

```

I think, perhaps there’s a typo I’m not able to resolve.

---

<div class="post-metadata">

**Author:** ![mseri](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mseri/32/91_2.png) [@mseri](https://discuss.ocaml.org/u/mseri)\
**Post date:** [October 31, 2018, 1:13pm UTC](https://discuss.ocaml.org/t/tutorial-for-cohttp-lwt-as-api-client/2746/12 "2018-10-31T13:13:11Z")

</div>

@abhi18av it is working fine in my `utop`. I can copy and paste the whole block, or each bit sequentially, and it is executed without errors. Are you sure `get_cards` was copied and pasted correctly?

---

<div class="post-metadata">

**Author:** ![abhi18av](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/abhi18av/32/1262_2.png) [@abhi18av](https://discuss.ocaml.org/u/abhi18av)\
**Post date:** [October 31, 2018, 2:32pm UTC](https://discuss.ocaml.org/t/tutorial-for-cohttp-lwt-as-api-client/2746/13 "2018-10-31T14:32:44Z")

</div>

Yup, I am quite sure that it has worked previously in my utop however, it’s only this time that it’s throwing up this error.

I have tried on a different machine and it’s the same type error.

Well, anyhow your code has been really helpful in helping me understand how to approach API clients in OCaml. A good next step, I feel is to package this up as a library for understanding how the package distribution ecosystem really works. Could you guide me here a bit ?

---

<div class="post-metadata">

**Author:** ![mseri](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mseri/32/91_2.png) [@mseri](https://discuss.ocaml.org/u/mseri)\
**Post date:** [October 31, 2018, 4:11pm UTC](https://discuss.ocaml.org/t/tutorial-for-cohttp-lwt-as-api-client/2746/14 "2018-10-31T16:11:08Z")

</div>

Sure. You can proceed as follows.

Create a new git repository for your project, put it e.g. on github. Let’s assume you just want to publish a CLI that runs the code that I had posted above. First remove the `#require` lines, all the double semicolons, and any other useless line. For readability you usually have the main entrypoint at the end of the file as `let () = ...`.

Put the code in `src/mything.ml` and create `democli.opam` and `src/dune`, the latter with the following content

```auto
(executable
 (name mything)
 (public_name democli)
 (libraries cohttp-lwt-unix lwt yojson))

```

You should now be able to build the executable with `dune build`. You now need to populate the opam file. Pick another file as a reference. Once that is written and you have committed all the changes, install `dune-release` with `opam`. Releasing the package is now a matter of `dune-release tag` and `dune-release`, see also [https://github.com/samoht/dune-release](https://github.com/samoht/dune-release)

---

<div class="post-metadata">

**Author:** ![cdaringe](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/cdaringe/32/2419_2.png) [@cdaringe](https://discuss.ocaml.org/u/cdaringe)\
**Post date:** [September 22, 2020, 3:23am UTC](https://discuss.ocaml.org/t/tutorial-for-cohttp-lwt-as-api-client/2746/15 "2020-09-22T03:23:55Z")

</div>

It’s two years later, so I thought I’d drop a note with great detailer for the next passer by for what worked well for me.

- `atdgen` is neat, but by default in their demo/instructions serializes your JSON to a binary format, requires `Biniou` (not optional, even though the instructions don’t suggest as much), and `Biniou` is not maintained.
- `yojson` seems to be active and the the hip thing. i installed `yojson` and [ppx\_deriving\_yojson](https://github.com/ocaml-ppx/ppx_deriving_yojson)

Draft your type:

```auto
(* MySweetData.ml *)
type my_sweet_data = {
  a: int;
  b: string;
} [@@deriving yojson];;
(* im actually in an ocaml/reason hybrid project, wasn't sure how to do this in reason syntax *)

```

Configure your build to support the ppx:

```auto
(library
 (name lib)
 ...
 (preprocess (pps ppx_deriving_yojson))

```

Then some nice serialization/deserialization functions _just show up_ conveniently:

```auto
MySweetData.my_sweet_data_of_yojson (* deserialize *)
MySweetData.my_sweet_data_to_yojson (* serialize *)

(* ... *)

MySweetData.my_sweet_data_of_yojson (Yojson.Safe.from_string some_json)

```

Donzo.
