Skip to main content
If you find a gap in your testing process that no k6 extension can fix, consider building your own extension.

Before you begin

Anyone who can use the command line to edit files and install software should be able to follow along. However, if you want to create an extension for production use, you should have:
  • Familiarity with both Go (Golang) and JavaScript, and their tooling
  • Understanding of how the Go-to-JavaScript bridge works within k6

Prerequisites

Install the required tools:
1

Install Go

Download and install Go (version 1.19 or higher).
2

Install Git

Install Git for version control.
3

Install xk6

Install the xk6 extension builder:
Verify your installation:

Avoid unneeded work

Before creating a new extension:
  • Check existing extensions - Review the Extensions catalog and the xk6 topic on GitHub
  • Prefer generic solutions - If you can test with a generic protocol like MQTT, use xk6-mqtt over building a custom protocol extension
  • Consider pure JavaScript - A JavaScript library will be better supported, more straightforward, and reusable than an extension

JavaScript extensions

JavaScript extensions extend the JavaScript APIs available to your test scripts. They can add support for new network protocols, improve performance, or add features.

Create a simple extension

1

Set up the project

Create a directory and initialize a Go module:
2

Write the Go code

Create compare.go with a struct that exposes methods:
3

Build the extension

Compile k6 with your extension:
4

Use in a test script

Create test.js:
Run the test:
k6 extensions must have the k6/x/ prefix, and the short name must be unique among all extensions built in the same k6 binary.

Advanced module API

For extensions that need access to internal k6 objects (VU state, runtime), implement the advanced module API:

Output extensions

Output extensions send metrics to a custom file format or service. They can add custom processing and dispatching methods.

Create an output extension

1

Set up the project

2

Implement the Output interface

Create logger.go:
3

Build and test

Secret source extensions

Secret source extensions provide secrets to your k6 script at runtime from external sources like HashiCorp Vault or AWS Secrets Manager.

Subcommand extensions

Subcommand extensions add custom CLI commands under the k6 x namespace for setup, validation, and integration tools.

Best practices

Performance

Code in the default function executes many times. Ensure operations are performant and thread-safe.

Initialization

Do heavy initialization in the init context, not in the default function.

Custom metrics

Use Registry.NewMetric() to create custom metrics and emit them with metrics.PushIfNotDone().

Templates

Use the xk6-example or xk6-output-example templates.

Quick start with templates

The fastest way to get started is using GitHub templates:
  1. Create a repository from grafana/xk6-example for JavaScript extensions
  2. Or use grafana/xk6-output-example for output extensions
  3. Open in GitHub Codespaces for instant development environment
  4. Run xk6 run script.js to test immediately

Registry requirements

If you maintain a public xk6 repository and wish to have your extension listed in the registry, review the requirements in the k6 documentation.

Next steps

Bundle Extensions

Build custom k6 binaries with xk6

Explore Extensions

See what others have built

Community Forum

Ask questions and share your extensions

GitHub Template

Start with the official template