How to make HTTP Requests with an Authorization Bearer Token in F# using FsHttp

Date: 2024-04-29 | create | tech | fsharp | http | options-scanner |

Most web projects will talk to other services using HTTP requests. Many services will require tokens or keys to identify and authorize requests to prevent abuse. One method for including tokens on HTTP requests is the Bearer Token.

F#'s FsHttp library allows you to make HTTP requests in an ergonomic, functional way. In this post we'll explore how to make an HTTP request with FsHttp that includes a Bearer Token.

Want to build fullstack apps with F#? I use the CloudSeed project boilerplate to jumpstart all my F# web projects.

FsHttp Example Project

To show how this works we'll be using an API that requires a bearer token. I'd thought about building a full project to demonstrate this but that seemed too complicated so instead we'll be using a real API in the real world.

The MarketData stock market API (referral) provides access to near real time stock market data. I recently used it to build my Stock Options Scanner app and it requires a bearer token for all non AAPL stock lookups which makes it a good candidate to demonstrate that this bearer token method actually works.


  • Configure API token
  • Configure stocks to pull


  • Pulls latest stock data for configured stocks using HTTP request with bearer


  • Received data

As we'll see:

  • With Bearer - all data
  • Without Bearer - only AAPL

Make an HTTP Request with a Bearer Token using FsHttp

Making a Bearer Token is easy - it's really just a string (similar to an API key) so all we have to do is add it to the request. We can do this with FsHttp by including the AuthorizationBearer attribute in our HTTP request builder.

let makeHttpRequest stockSymbol = 
  http {
      GET (createMarketDataQuoteUrl stockSymbol)
      AuthorizationBearer marketDataToken
  |> Request.send
  |> Response.toJson

This code:

  • Creates a request to the MarketData API and adds the Bearer Token
  • Sends the Request
  • Converts the response to JSON

We can prove this works by looking at the responses with and without the bearer token - here looking up AAPL and MSFT stocks.

With an empty token - we can only get the AAPL data, MSFT fails.

MarketData - No bearer token

With a valid token - API gives us all the stock data we asked for.

MarketData - includes bearer token

Full Source Code

You can find the full source code in my Replit instance (though you'll have to get your own API key, sorry).

Here's a snapshot of that code for posterity:

open System
open FsHttp

// You must input your token here to auth
let marketDataToken = ""

let stocksToPull = 
    "AAPL" // Free - no token needed
    "MSFT" // Not free - token needed

let createMarketDataQuoteUrl
  (stockSymbol: string)
  : string 

let makeHttpRequest stockSymbol = 
  http {
      GET (createMarketDataQuoteUrl stockSymbol)
      AuthorizationBearer marketDataToken
  |> Request.send
  |> Response.toJson

let main argv =
    printfn "Hello World from F#!"

    |> List.iter (
      fun s -> 
        let response = makeHttpRequest s
        printfn "Stock: %A" s 
        printfn "* data: %A" response
    0 // return an integer exit code


If you're curious I've written about How I built a Stock Options Scanner in F# and HAMINIONs members can get full access to its source code.

Q: What are you struggling with / curious about around building fullstack apps with F#?

If you liked this post you might also like:

Want more like this?

The best / easiest way to support my work is by subscribing for future updates and sharing with your network.