Skip to main content
Create custom k6 extensions to add JavaScript modules, output formats, secret sources, or CLI subcommands. Extensions are written in Go and compiled into k6 using xk6.

Prerequisites

  • Go 1.22 or later
  • Understanding of Go programming
  • Familiarity with k6 concepts
  • Git for version control

Extension Types

You can create four types of extensions:
  1. JavaScript Extensions: Add custom JavaScript APIs
  2. Output Extensions: Create custom result outputs
  3. Secret Source Extensions: Implement secret providers
  4. Subcommand Extensions: Add CLI subcommands
See Extensions Overview for details on each type.

Project Setup

Follow the xk6- naming convention for discoverability. Name your extension repository xk6-extensionname.

Creating a JavaScript Extension

JavaScript extensions add custom modules that can be imported in test scripts.

Basic Structure

Adding Functionality

Add methods that will be available in JavaScript:
Use in JavaScript:

Working with Metrics

Create custom metrics in your extension:
Always check if vu.State() is nil. It will be nil in the init context where metric emission is not allowed.

Real Example

Here’s a real JavaScript extension from k6’s test suite (xk6-js-test/jstest.go:1):

Creating an Output Extension

Output extensions send test results to custom backends.

Basic Implementation

See Creating Custom Outputs for a comprehensive guide. Quick example:

Creating a Secret Source Extension

Secret source extensions provide secure configuration values.

Implementation

Creating a Subcommand Extension

Subcommand extensions add new CLI commands to k6.

Implementation

Use it:

Extension Registration

All extensions must register in an init() function using ext.Register() (ext/ext.go:62):
Registration happens automatically when the extension package is imported during build.

Testing Your Extension

Unit Tests

Integration Tests

Create a test script:
Build and test:

Building and Using

Local Development

Build k6 with your local extension:

Publishing

Best Practices

Common Patterns

Connection Pooling

Configuration

Resource Cleanup

Debugging

Enable Verbose Logging

Use k6 Verbose Mode

Example Extensions

Study these official examples:

Resources

Next Steps