package reddit_api_async

  1. Overview
  2. Docs
Async connection and utility functions for Reddit's API


Dune Dependency





reddit_api is a set OCaml client libraries for Reddit's API.

reddit_api_kernel provides:

  • Types for representing Reddit's API parameters and responses

  • Functions for building HTTP requests and handling the associated responses for Reddit's API endpoints.

reddit_api_async provides a client for sending these requests to Reddit and some utilities for common usage patterns. It handles authentication and Reddit's rate-limiting headers.


Here. I recommend the Reddit_api_kernel.Api and Reddit_api_async.Connection modules as entry points.


let print_links credentials =
  let connection = Connection.create credentials ~user_agent:"Link printer" in
  let subreddit =
    Subreddit_name.combine ( ~f:Subreddit_name.of_string [ "ocaml"; "redditdev" ])
  let%bind link_listing =
    Connection.call_exn connection ( ~since:Year ~subreddit ())
  let links = Listing.children link_listing in
  List.iter links ~f:(fun link ->
          { title : string = Thing.Link.title link
          ; url : string option = (Thing.Link.url link) ~f:Uri.to_string
          ; author : Username.t option = link
          ; score : int = Thing.Link.score link
  return ()

Goals and non-goals


  • Provide a typed interface to Reddit's API endpoints and responses.

  • Encode knowledge, documented or otherwise, about correct usage of the API via the type system.

    • Don't raise exceptions if Reddit is behaving "as expected."

    • Express corner cases in response types. For example, are there surprising cases where a field might not be present? Make the field optional instead of making each user discover this on their own.

    • Handle common Reddit server issues such as 503 errors automatically, or else warn about them via the response type.

  • Provide workarounds when we get the above wrong:

    • A ?param_list_override:((string * string list) list -> (string * string list) list) option on each API endpoint that allows the HTTP parameters to be manipulated directly.

    • A Connection.call_raw function that allows users to access HTTP responses directly.


  • Be perfect

    • Reddit's API is not very well documented. Determining which inputs and outputs are legal is largely a matter of trial and error. At any given time, it's likely that we allow some invalid combination of inputs, or forbid a valid combination, or fail to handle some valid response.

  • Express "unexpected" Reddit behavior in the type system.

    • If we get something from Reddit that we don't understand, we'll just raise. We don't make every function return a (_, [`couldn't_parse_response]) Result.t).


Thanks to PRAW for providing innumerable examples of Reddit API client code.


Innovation. Community. Security.