Search APIs: Brave Search

Giving a program web search turns it from a closed system into one that can look things up. This chapter builds a client for the Brave Search API, in source-code/brave_search/. It sends a query and prints the original query plus one line per result. The structure is the one from the last chapter: a pure codec you can test offline, and a thin Lwt wrapper for the HTTP call.

Modeling a Result

A search result has many fields, and any of them can be missing from a given response. That is a job for the option type. The file lib/brave.ml models one web result as a record where the optional fields have option types:

1 type web_result = {
2   type_ : string;
3   index : int option;
4   all : bool option;
5   title : string option;
6   url : string option;
7   description : string option;
8 }

Only type_ is always present; the rest are option, so a result with no description holds None there rather than an empty string or a crash. The field is named type_ because type is a reserved word in OCaml.

Decoding Optional Fields

Decoding these fields needs a small helper, because each one might be JSON null. The opt function takes a converter and returns None for null or Some value otherwise:

1 let opt conv = function `Null -> None | v -> Some (conv v)

With opt in hand, decoding a whole result reads cleanly. Each line pipes the field through opt and the right converter:

 1 let web_result_of_json json =
 2   let open Yojson.Safe.Util in
 3   {
 4     type_ = json |> member "type" |> to_string;
 5     index = json |> member "index" |> opt to_int;
 6     all = json |> member "all" |> opt to_bool;
 7     title = json |> member "title" |> opt to_string;
 8     url = json |> member "url" |> opt to_string;
 9     description = json |> member "description" |> opt to_string;
10   }

opt to_string reads a string or gives None; opt to_int and opt to_bool do the same for numbers and booleans. This little combinator replaces the special optional-field accessor a JSON library might give you, and it works for any type.

Formatting a Result

Turning a result into a readable line means handling the missing fields. format_result prints each field or N/A, and trims a long description to 100 characters:

 1 let format_result r =
 2   let field label = function Some v -> label ^ v | None -> "N/A" in
 3   let title = field "Title: " r.title in
 4   let url = field "URL: " r.url in
 5   let desc =
 6     match r.description with
 7     | Some d ->
 8       let d = if String.length d > 100 then String.sub d 0 100 else d in
 9       "Description: " ^ d
10     | None -> "N/A"
11   in
12   String.concat " | " [ title; url; desc ]

The local field helper takes a label and an option, and returns the labeled value or N/A. Decoding a whole response gives the query line followed by one formatted line per result:

1 let suggestions_of_json json =
2   let open Yojson.Safe.Util in
3   let original = json |> member "query" |> member "original" |> to_string in
4   let results =
5     json |> member "web" |> member "results" |> to_list
6     |> List.map web_result_of_json
7   in
8   ("Original Query: " ^ original) :: List.map format_result results

The HTTP Call

The program in bin/main.ml sends the query. Brave takes the search terms as URL query parameters and the API key as a header. Uri.add_query_params' builds the URL and encodes the terms:

 1 let get_search_suggestions ~api_key query =
 2   let uri =
 3     Uri.add_query_params'
 4       (Uri.of_string endpoint)
 5       [ ("q", query); ("country", "US"); ("count", "5") ]
 6   in
 7   let headers =
 8     Cohttp.Header.of_list
 9       [ ("Accept", "application/json"); ("X-Subscription-Token", api_key) ]
10   in
11   Lwt.catch
12     (fun () ->
13       let* resp, body = Cohttp_lwt_unix.Client.get ~headers uri in
14       let* body_str = Cohttp_lwt.Body.to_string body in
15       let code = Cohttp.Code.code_of_status (Cohttp.Response.status resp) in
16       if code <> 200 then Lwt.return (Error (Printf.sprintf "HTTP error: %d" code))
17       else
18         match Yojson.Safe.from_string body_str with
19         | json -> Lwt.return (Ok (Brave.suggestions_of_json json))
20         | exception e ->
21           Lwt.return (Error ("JSON parsing error: " ^ Printexc.to_string e)))
22     (fun exn -> Lwt.return (Error ("Network error: " ^ Printexc.to_string exn)))

The key goes in the X-Subscription-Token header. The call handles three kinds of failure, each as a distinct Error: a non-200 status, a JSON parse error (caught with the exception e case in the match), and a network exception (caught by Lwt.catch). The main function reads the query from the command line and the key from the environment:

1 let () =
2   let query =
3     match Array.to_list Sys.argv with _ :: q :: _ -> q | _ -> "OCaml programming"
4   in
5   match Sys.getenv_opt "BRAVE_SEARCH_API_KEY" with
6   | None ->
7     print_endline "Error: BRAVE_SEARCH_API_KEY environment variable not set.";
8     exit 1
9   | Some api_key -> ( ... )

Running It

Set your API token and run a search:

1 cd source-code/brave_search
2 export BRAVE_SEARCH_API_KEY=your-token
3 dune build
4 dune exec ./bin/main.exe -- "OCaml programming"
5 dune runtest

The program prints the original query and up to five results, one per line. Because the decode lives in brave.ml and takes a parsed JSON value, the tests decode a saved sample response offline, so dune runtest needs no token and no network. The opt combinator you saw here is worth keeping: whenever you decode JSON with fields that may be absent, one small opt conv function handles them all.