Ktor 3.4.0 Help

Client plugins

Many applications require common functionality that is not part of the core application logic, such as logging, serialization, or authorization. In Ktor, this functionality is provided by client plugins.

Add plugin dependencies

Some plugins require an additional dependency. For example, to use the Logging plugin, you need to add the ktor-client-logging artifact in your build script:

implementation("io.ktor:ktor-client-logging:$ktor_version")
implementation "io.ktor:ktor-client-logging:$ktor_version"
<dependency> <groupId>io.ktor</groupId> <artifactId>ktor-client-logging-jvm</artifactId> <version>${ktor_version}</version> </dependency>

Each plugin’s documentation specifies any required dependencies.

Install a plugin

To install a plugin, pass it to the install() function inside a client configuration block.

For example, installing the Logging plugin looks as follows:

import io.ktor.client.* import io.ktor.client.engine.cio.* import io.ktor.client.plugins.logging.* val client = HttpClient(CIO) { install(Logging) }

Install or replace a plugin

In some cases, a plugin may already be installed — for example, by shared client configuration code. In such cases, you can replace its configuration using the installOrReplace() function:

val client = HttpClient(CIO) { installOrReplace(ContentNegotiation) { // ... } }

This function installs the plugin if it is not present or replaces its existing configuration if it has already been installed.

Configure a plugin

Most plugins expose configuration options that can be set inside the install block.

For example, the Logging plugin allows you to specify the logger, logging level, and condition for filtering log messages:

val client = HttpClient(CIO) { install(Logging) { logger = Logger.DEFAULT level = LogLevel.HEADERS filter { request -> request.url.host.contains("ktor.io") } sanitizeHeader { header -> header == HttpHeaders.Authorization } } }

Create a custom plugin

If the existing plugins do not meet your needs, you can create your own custom client plugins. Custom plugins allow you to intercept requests and responses and implement reusable behavior.

To learn more, see Custom client plugins.

19 December 2025