Using as an sttp client

Add the dependency:

"com.softwaremill.sttp.tapir" %% "tapir-sttp-client" % "0.19.2"

To make requests using an endpoint definition using the sttp client, import:

import sttp.tapir.client.sttp.SttpClientInterpreter

This objects contains four methods:

  • toRequestUnsafe(PublicEndpoint, Uri): given a public endpoint and the base URI returns a function, which might throw an exception if decoding of the result fails

    I => Request[Either[E, O], Any]
    
  • toRequest(PublicEndpoint, Uri): given a public endpoint and the base URI returns a function, which represents decoding errors as the DecodeResult class

    I => Request[DecodeResult[Either[E, O]], Any]
    
  • toSecureRequestUnsafe(Endpoint, Uri): given a secure endpoint and the base URI returns a function, which might throw an exception if decoding of the result fails

    A => I => Request[Either[E, O], Any]
    
  • toSecureRequest(Endpoint, Uri): given a secure endpoint and the base URI returns a function, which represents decoding errors as the DecodeResult class

    A => I => Request[DecodeResult[Either[E, O]], Any]
    

Note that the returned functions have one argument each: first the security inputs (if any), and regular input values of the endpoint. This might be a single type, a tuple, or a case class, depending on the endpoint description.

After providing the input parameters, a description of the request to be made is returned, with the input value encoded as appropriate request parameters: path, query, headers and body. This can be further customised and sent using any sttp backend. The response will then contain the decoded error or success values (note that this can be the body enriched with data from headers/status code).

See the runnable example for example usage.

Web sockets

To interpret a web socket endpoint, an additional streams-specific import is needed, so that the interpreter can convert sttp’s WebSocket instance into a pipe. This logic is looked up via the WebSocketToPipe implicit.

The required imports are as follows:

import sttp.tapir.client.sttp.ws.akkahttp._ // for akka-streams // for akka-streams
import sttp.tapir.client.sttp.ws.fs2._      // for fs2      // for fs2
import sttp.tapir.client.sttp.ws.zio._      // for zio

No additional dependencies are needed, as both of the above implementations are included in the main interpreter, with dependencies on akka-streams, fs2 and zio being marked as optional (hence these are not transitive).

Scala.JS

In this case add the following dependencies (note the %%% instead of the usual %%):

"com.softwaremill.sttp.tapir" %%% "tapir-sttp-client" % "0.19.2"
"io.github.cquiroz" %%% "scala-java-time" % "2.2.0" // implementations of java.time classes for Scala.JS

The client interpreter also supports Scala.JS, the request must then be sent using the sttp client Scala.JS Fetch backend.

You can check the SttpClientTests for a working example.