---
title: "JavaScript API | Grafana k6 documentation"
description: "JavaScript API The list of k6 modules natively supported in your k6 scripts. Init context Before the k6 starts the test logic, code in the init context prepares the script. A few functions are available only in init context. For details about the runtime, refer to the Test lifecycle."
---

# JavaScript API

The list of k6 modules natively supported in your k6 scripts.

## Init context

Before the k6 starts the test logic, code in the *init context* prepares the script. A few functions are available only in init context. For details about the runtime, refer to the [Test lifecycle](/docs/k6/latest/using-k6/test-lifecycle/).

Expand table

| Function                                                                        | Description                                          |
|---------------------------------------------------------------------------------|------------------------------------------------------|
| [open( filePath, \[mode\] )](/docs/k6/latest/javascript-api/init-context/open/) | Opens a file and reads all the contents into memory. |

## import.meta

The `import.meta` object provides the `resolve` function, which resolves a path to a URL string in the same way an import statement does.

The `import.meta` object is only available in ECMAScript modules, not in CommonJS modules.

Expand table

| Function                                                                   | Description                                                            |
|----------------------------------------------------------------------------|------------------------------------------------------------------------|
| [import.meta.resolve](/docs/k6/latest/javascript-api/import.meta/resolve/) | Resolves a path to a URL string the same way an import statement does. |

## k6

The [`k6` module](/docs/k6/latest/javascript-api/k6/) contains k6-specific functionality.

Expand table

| Function                                                               | Description                                                                                                                                  |
|------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------|
| [check(val, sets, \[tags\])](/docs/k6/latest/javascript-api/k6/check/) | Runs one or more checks on a value and generates a pass/fail result but does not throw errors or otherwise interrupt execution upon failure. |
| [fail(\[err\])](/docs/k6/latest/javascript-api/k6/fail/)               | Throws an error, failing and aborting the current VU script iteration immediately.                                                           |
| [group(name, fn)](/docs/k6/latest/javascript-api/k6/group/)            | Runs code inside a group. Used to organize results in a test.                                                                                |
| [randomSeed(int)](/docs/k6/latest/javascript-api/k6/random-seed/)      | Set seed to get a reproducible pseudo-random number using `Math.random`.                                                                     |
| [sleep(t)](/docs/k6/latest/javascript-api/k6/sleep/)                   | Suspends VU execution for the specified duration.                                                                                            |

## k6/browser

The [`k6/browser` module](/docs/k6/latest/javascript-api/k6-experimental/) provides browser-level APIs to interact with browsers and collect frontend performance metrics as part of your k6 tests.

Expand table

| Method                                                                                                                                         | Description                                                                                                                                                                                                                                                                                                                                                                              |
|------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [browser.closeContext()](/docs/k6/latest/javascript-api/k6-browser/closecontext/)                                                              | Closes the current [BrowserContext](/docs/k6/latest/javascript-api/k6-browser/browsercontext/).                                                                                                                                                                                                                                                                                          |
| [browser.context()](/docs/k6/latest/javascript-api/k6-browser/context/)                                                                        | Returns the current [BrowserContext](/docs/k6/latest/javascript-api/k6-browser/browsercontext/).                                                                                                                                                                                                                                                                                         |
| [browser.isConnected](/docs/k6/latest/javascript-api/k6-browser/isconnected/) [](https://github.com/grafana/xk6-browser/issues/453)            | Indicates whether the [CDP](https://chromedevtools.github.io/devtools-protocol/) connection to the browser process is active or not.                                                                                                                                                                                                                                                     |
| [browser.newContext(\[options\])](/docs/k6/latest/javascript-api/k6-browser/newcontext/) [](https://github.com/grafana/xk6-browser/issues/455) | Creates and returns a new [BrowserContext](/docs/k6/latest/javascript-api/k6-browser/browsercontext/).                                                                                                                                                                                                                                                                                   |
| [browser.newPage(\[options\])](/docs/k6/latest/javascript-api/k6-browser/newpage/) [](https://github.com/grafana/xk6-browser/issues/455)       | Creates a new [Page](/docs/k6/latest/javascript-api/k6-browser/page/) in a new [BrowserContext](/docs/k6/latest/javascript-api/k6-browser/browsercontext/) and returns the page. Pages that have been opened ought to be closed using [`Page.close`](/docs/k6/latest/javascript-api/k6-browser/page/close/). Pages left open could potentially distort the results of Web Vital metrics. |
| [browser.version()](/docs/k6/latest/javascript-api/k6-browser/version/)                                                                        | Returns the browser application’s version.                                                                                                                                                                                                                                                                                                                                               |

Expand table

| k6 Class                                                                    | Description                                                                                                                      |
|-----------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------|
| [BrowserContext](/docs/k6/latest/javascript-api/k6-browser/browsercontext/) | Enables independent browser sessions with separate [Page](/docs/k6/latest/javascript-api/k6-browser/page/)s, cache, and cookies. |
| [ElementHandle](/docs/k6/latest/javascript-api/k6-browser/elementhandle/)   | Represents an in-page DOM element.                                                                                               |
| [Frame](/docs/k6/latest/javascript-api/k6-browser/frame/)                   | Access and interact with the [`Page`](/docs/k6/latest/javascript-api/k6-browser/page/).’s `Frame`s.                              |
| [JSHandle](/docs/k6/latest/javascript-api/k6-browser/jshandle/)             | Represents an in-page JavaScript object.                                                                                         |
| [Keyboard](/docs/k6/latest/javascript-api/k6-browser/keyboard/)             | Used to simulate the keyboard interactions with the associated [`Page`](/docs/k6/latest/javascript-api/k6-browser/page/).        |
| [Locator](/docs/k6/latest/javascript-api/k6-browser/locator/)               | The Locator API makes it easier to work with dynamically changing elements.                                                      |
| [Mouse](/docs/k6/latest/javascript-api/k6-browser/mouse/)                   | Used to simulate the mouse interactions with the associated [`Page`](/docs/k6/latest/javascript-api/k6-browser/page/).           |
| [Page](/docs/k6/latest/javascript-api/k6-browser/page/)                     | Provides methods to interact with a single tab in a browser.                                                                     |
| [Request](/docs/k6/latest/javascript-api/k6-browser/request/)               | Used to keep track of the request the [`Page`](/docs/k6/latest/javascript-api/k6-browser/page/) makes.                           |
| [Response](/docs/k6/latest/javascript-api/k6-browser/response/)             | Represents the response received by the [`Page`](/docs/k6/latest/javascript-api/k6-browser/page/).                               |
| [Touchscreen](/docs/k6/latest/javascript-api/k6-browser/touchscreen/)       | Used to simulate touch interactions with the associated [`Page`](/docs/k6/latest/javascript-api/k6-browser/page/).               |
| [Worker](/docs/k6/latest/javascript-api/k6-browser/worker/)                 | Represents a [WebWorker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API).                                      |

## k6/crypto

The [`k6/crypto` module](/docs/k6/latest/javascript-api/k6-crypto/) provides common hashing functionality available in the GoLang [crypto](https://golang.org/pkg/crypto/) package.

Expand table

| Function                                                                                        | Description                                                                                                                  |
|-------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------|
| [createHash(algorithm)](/docs/k6/latest/javascript-api/k6-crypto/createhash/)                   | Create a Hasher object, allowing the user to add data to hash multiple times, and extract hash digests along the way.        |
| [createHMAC(algorithm, secret)](/docs/k6/latest/javascript-api/k6-crypto/createhmac/)           | Create an HMAC hashing object, allowing the user to add data to hash multiple times, and extract hash digests along the way. |
| [hmac(algorithm, secret, data, outputEncoding)](/docs/k6/latest/javascript-api/k6-crypto/hmac/) | Use HMAC to sign an input string.                                                                                            |
| [md4(input, outputEncoding)](/docs/k6/latest/javascript-api/k6-crypto/md4/)                     | Use MD4 to hash an input string.                                                                                             |
| [md5(input, outputEncoding)](/docs/k6/latest/javascript-api/k6-crypto/md5/)                     | Use MD5 to hash an input string.                                                                                             |
| [randomBytes(int)](/docs/k6/latest/javascript-api/k6-crypto/randombytes/)                       | Return an array with a number of cryptographically random bytes.                                                             |
| [ripemd160(input, outputEncoding)](/docs/k6/latest/javascript-api/k6-crypto/ripemd160/)         | Use RIPEMD-160 to hash an input string.                                                                                      |
| [sha1(input, outputEncoding)](/docs/k6/latest/javascript-api/k6-crypto/sha1/)                   | Use SHA-1 to hash an input string.                                                                                           |
| [sha256(input, outputEncoding)](/docs/k6/latest/javascript-api/k6-crypto/sha256/)               | Use SHA-256 to hash an input string.                                                                                         |
| [sha384(input, outputEncoding)](/docs/k6/latest/javascript-api/k6-crypto/sha384/)               | Use SHA-384 to hash an input string.                                                                                         |
| [sha512(input, outputEncoding)](/docs/k6/latest/javascript-api/k6-crypto/sha512/)               | Use SHA-512 to hash an input string.                                                                                         |
| [sha512\_224(input, outputEncoding)](/docs/k6/latest/javascript-api/k6-crypto/sha512_224/)      | Use SHA-512/224 to hash an input string.                                                                                     |
| [sha512\_256(input, outputEncoding)](/docs/k6/latest/javascript-api/k6-crypto/sha512_256/)      | Use SHA-512/256 to hash an input string.                                                                                     |

Expand table

| Class                                                      | Description                                                                                                                                                                   |
|------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Hasher](/docs/k6/latest/javascript-api/k6-crypto/hasher/) | Object returned by [crypto.createHash()](/docs/k6/latest/javascript-api/k6-crypto/createhash/). It allows adding more data to be hashed and to extract digests along the way. |

## k6/data

The [`k6/data` module](/docs/k6/latest/javascript-api/k6-data/) provides helpers to work with data.

Expand table

| Class/Method                                                       | Description                                                   |
|--------------------------------------------------------------------|---------------------------------------------------------------|
| [SharedArray](/docs/k6/latest/javascript-api/k6-data/sharedarray/) | read-only array like structure that shares memory between VUs |

## k6/encoding

The [`k6/encoding` module](/docs/k6/latest/javascript-api/k6-encoding/) provides [base64](https://en.wikipedia.org/wiki/Base64) encoding/decoding as defined by [RFC4648](https://tools.ietf.org/html/rfc4648).

Expand table

| Function                                                                                            | Description             |
|-----------------------------------------------------------------------------------------------------|-------------------------|
| [b64decode(input, \[encoding\], \[format\])](/docs/k6/latest/javascript-api/k6-encoding/b64decode/) | Base64 decode a string. |
| [b64encode(input, \[encoding\])](/docs/k6/latest/javascript-api/k6-encoding/b64encode/)             | Base64 encode a string. |

## k6/execution

The [`k6/execution` module](/docs/k6/latest/javascript-api/k6-execution/) provides the capability to get information about the current test execution state inside the test script. You can read in your script the execution state during the test execution and change your script logic based on the current state.

`k6/execution` provides the test execution information with the following properties:

- [instance](/docs/k6/latest/javascript-api/k6-execution/#instance)
- [scenario](/docs/k6/latest/javascript-api/k6-execution/#scenario)
- [test](/docs/k6/latest/javascript-api/k6-execution/#test)
- [vu](/docs/k6/latest/javascript-api/k6-execution/#vu)

## k6/experimental

[`k6/experimental` modules](/docs/k6/latest/javascript-api/k6-experimental/) are stable modules that may introduce breaking changes. Once they become fully stable, they may graduate to become k6 core modules.

Expand table

| Modules                                                            | Description                                                                                                                |
|--------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------|
| [csv](/docs/k6/latest/javascript-api/k6-experimental/csv/)         | Provides support for efficient and convenient parsing of CSV files.                                                        |
| [fs](/docs/k6/latest/javascript-api/k6-experimental/fs/)           | Provides a memory-efficient way to handle file interactions within your test scripts.                                      |
| [streams](/docs/k6/latest/javascript-api/k6-experimental/streams/) | Provides an implementation of the Streams API specification, offering support for defining and consuming readable streams. |

## k6/html

The [`k6/html` module](/docs/k6/latest/javascript-api/k6-html/) contains functionality for HTML parsing.

Expand table

| Function                                                            | Description                                                                                                |
|---------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------|
| [parseHTML(src)](/docs/k6/latest/javascript-api/k6-html/parsehtml/) | Parse an HTML string and populate a [Selection](/docs/k6/latest/javascript-api/k6-html/selection/) object. |

Expand table

| Class                                                          | Description                                                                                                |
|----------------------------------------------------------------|------------------------------------------------------------------------------------------------------------|
| [Element](/docs/k6/latest/javascript-api/k6-html/element/)     | An HTML DOM element as returned by the [Selection](/docs/k6/latest/javascript-api/k6-html/selection/) API. |
| [Selection](/docs/k6/latest/javascript-api/k6-html/selection/) | A jQuery-like API for accessing HTML DOM elements.                                                         |

## k6/http

The [`k6/http` module](/docs/k6/latest/javascript-api/k6-http/) contains functionality for performing HTTP transactions.

Expand table

| Function                                                                                                  | Description                                                                                                       |
|-----------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------|
| [batch( requests )](/docs/k6/latest/javascript-api/k6-http/batch/)                                        | Issue multiple HTTP requests in parallel (like e.g. browsers tend to do).                                         |
| [cookieJar()](/docs/k6/latest/javascript-api/k6-http/cookiejar-method/)                                   | Get active HTTP Cookie jar.                                                                                       |
| [del( url, \[body\], \[params\] )](/docs/k6/latest/javascript-api/k6-http/del/)                           | Issue an HTTP DELETE request.                                                                                     |
| [file( data, \[filename\], \[contentType\] )](/docs/k6/latest/javascript-api/k6-http/file/)               | Create a file object that is used for building multi-part requests.                                               |
| [get( url, \[params\] )](/docs/k6/latest/javascript-api/k6-http/get/)                                     | Issue an HTTP GET request.                                                                                        |
| [head( url, \[params\] )](/docs/k6/latest/javascript-api/k6-http/head/)                                   | Issue an HTTP HEAD request.                                                                                       |
| [options( url, \[body\], \[params\] )](/docs/k6/latest/javascript-api/k6-http/options/)                   | Issue an HTTP OPTIONS request.                                                                                    |
| [patch( url, \[body\], \[params\] )](/docs/k6/latest/javascript-api/k6-http/patch/)                       | Issue an HTTP PATCH request.                                                                                      |
| [post( url, \[body\], \[params\] )](/docs/k6/latest/javascript-api/k6-http/post/)                         | Issue an HTTP POST request.                                                                                       |
| [put( url, \[body\], \[params\] )](/docs/k6/latest/javascript-api/k6-http/put/)                           | Issue an HTTP PUT request.                                                                                        |
| [request( method, url, \[body\], \[params\] )](/docs/k6/latest/javascript-api/k6-http/request/)           | Issue any type of HTTP request.                                                                                   |
| [asyncRequest( method, url, \[body\], \[params\] )](/docs/k6/latest/javascript-api/k6-http/asyncrequest/) | Issue any type of HTTP request asynchronously.                                                                    |
| [setResponseCallback(expectedStatuses)](/docs/k6/latest/javascript-api/k6-http/set-response-callback/)    | Sets a response callback to mark responses as expected.                                                           |
| [url\`url\`](/docs/k6/latest/javascript-api/k6-http/url/)                                                 | Creates a URL with a name tag. Read more on [URL Grouping](/docs/k6/latest/using-k6/http-requests/#url-grouping). |
| [expectedStatuses( statusCodes )](/docs/k6/latest/javascript-api/k6-http/expected-statuses/)              | Create a callback for setResponseCallback that checks status codes.                                               |

Expand table

| Class                                                          | Description                                                                              |
|----------------------------------------------------------------|------------------------------------------------------------------------------------------|
| [CookieJar](/docs/k6/latest/javascript-api/k6-http/cookiejar/) | Used for storing cookies, set by the server and/or added by the client.                  |
| [FileData](/docs/k6/latest/javascript-api/k6-http/filedata/)   | Used for wrapping data representing a file when doing multipart requests (file uploads). |
| [Params](/docs/k6/latest/javascript-api/k6-http/params/)       | Used for setting various HTTP request-specific parameters such as headers, cookies, etc. |
| [Response](/docs/k6/latest/javascript-api/k6-http/response/)   | Returned by the http.* methods that generate HTTP requests.                              |

## k6/metrics

The [`k6/metrics` module](/docs/k6/latest/javascript-api/k6-metrics/) provides functionality to [create custom metrics](/docs/k6/latest/using-k6/metrics/create-custom-metrics/) of various types.

Expand table

| Metric type                                                   | Description                                                                                   |
|---------------------------------------------------------------|-----------------------------------------------------------------------------------------------|
| [Counter](/docs/k6/latest/javascript-api/k6-metrics/counter/) | A metric that cumulatively sums added values.                                                 |
| [Gauge](/docs/k6/latest/javascript-api/k6-metrics/gauge/)     | A metric that stores the min, max and last values added to it.                                |
| [Rate](/docs/k6/latest/javascript-api/k6-metrics/rate/)       | A metric that tracks the percentage of added values that are non-zero.                        |
| [Trend](/docs/k6/latest/javascript-api/k6-metrics/trend/)     | A metric that calculates statistics on the added values (min, max, average, and percentiles). |

## k6/net/grpc

The [`k6/net/grpc` module](/docs/k6/latest/javascript-api/k6-net-grpc/) provides a [gRPC](https://grpc.io/) client for Remote Procedure Calls (RPC) over HTTP/2.

Expand table

| Class/Method                                                                                                           | Description                                                                                                                                                 |
|------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Client](/docs/k6/latest/javascript-api/k6-net-grpc/client/)                                                           | gRPC client used for making RPC calls to a gRPC Server.                                                                                                     |
| [Client.load(importPaths, …protoFiles)](/docs/k6/latest/javascript-api/k6-net-grpc/client/client-load/)                | Loads and parses the given protocol buffer definitions to be made available for RPC requests.                                                               |
| [Client.connect(address \[,params\])](/docs/k6/latest/javascript-api/k6-net-grpc/client/client-connect/)               | Connects to a given gRPC service.                                                                                                                           |
| [Client.invoke(url, request \[,params\])](/docs/k6/latest/javascript-api/k6-net-grpc/client/client-invoke/)            | Makes an unary RPC for the given service/method and returns a [Response](/docs/k6/latest/javascript-api/k6-net-grpc/response/).                             |
| [Client.asyncInvoke(url, request \[,params\])](/docs/k6/latest/javascript-api/k6-net-grpc/client/client-async-invoke/) | Asynchronously makes an unary RPC for the given service/method and returns a Promise with [Response](/docs/k6/latest/javascript-api/k6-net-grpc/response/). |
| [Client.close()](/docs/k6/latest/javascript-api/k6-net-grpc/client/client-close/)                                      | Close the connection to the gRPC service.                                                                                                                   |
| [Params](/docs/k6/latest/javascript-api/k6-net-grpc/params/)                                                           | RPC Request specific options.                                                                                                                               |
| [Response](/docs/k6/latest/javascript-api/k6-net-grpc/response/)                                                       | Returned by RPC requests.                                                                                                                                   |
| [Constants](/docs/k6/latest/javascript-api/k6-net-grpc/constants/)                                                     | Define constants to distinguish between [gRPC Response](/docs/k6/latest/javascript-api/k6-net-grpc/response/) statuses.                                     |
| [Stream(client, url, \[,params\])](/docs/k6/latest/javascript-api/k6-net-grpc/stream/)                                 | Creates a new gRPC stream.                                                                                                                                  |
| [Stream.on(event, handler)](/docs/k6/latest/javascript-api/k6-net-grpc/stream/stream-on/)                              | Adds a new listener to one of the possible stream events.                                                                                                   |
| [Stream.write(message)](/docs/k6/latest/javascript-api/k6-net-grpc/stream/stream-write/)                               | Writes a message to the stream.                                                                                                                             |
| [Stream.end()](/docs/k6/latest/javascript-api/k6-net-grpc/stream/stream-end/)                                          | Signals to the server that the client has finished sending.                                                                                                 |
| [EventHandler](/docs/k6/latest/javascript-api/k6-net-grpc/stream/event-handler/)                                       | The function to call for various events on the gRPC stream.                                                                                                 |
| [Metadata](/docs/k6/latest/javascript-api/k6-net-grpc/stream/message-metadata/)                                        | The metadata of a gRPC stream’s message.                                                                                                                    |

## k6/secrets

The [`k6/secrets` module](/docs/k6/latest/javascript-api/k6-secrets/) gives access to secrets provided by configured [secret sources](/docs/k6/latest/using-k6/secret-source/).

Expand table

| Property                                                                | Description                                                                                         |
|-------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------|
| [get(\[String\])](/docs/k6/latest/javascript-api/k6-secrets/#get)       | asynchrounsly get a secret from the default secret source                                           |
| [source(\[String\])](/docs/k6/latest/javascript-api/k6-secrets/#source) | returns a source for the provided name that can than be used to get a secret from a concrete source |

## k6/timers

The [`k6/timers` module](/docs/k6/latest/javascript-api/k6-timers/) implements timers to work with k6’s event loop. They mimic the functionality found in browsers and other JavaScript runtimes.

Expand table

| Function                                                                      | Description                                          |
|-------------------------------------------------------------------------------|------------------------------------------------------|
| [setTimeout](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout)     | Sets a function to be run after a given timeout.     |
| [clearTimeout](https://developer.mozilla.org/en-US/docs/Web/API/clearTimeout) | Clears a previously set timeout with `setTimeout`.   |
| [setInterval](https://developer.mozilla.org/en-US/docs/Web/API/setInterval)   | Sets a function to be run on a given interval.       |
| [clearInterval](https://developer.mozilla.org/en-US/docs/Web/API/setInterval) | Clears a previously set interval with `setInterval`. |

> Note
> 
> The timer methods are available globally, so you can use them in your script without including an import statement.

## k6/websockets

The [`k6/websockets` module](/docs/k6/latest/javascript-api/k6-websockets/) provides an implementation of the WebSocket API for k6 with k6-specific features like cookies, tags, and headers.

Expand table

| Class/Method                                                                                                                     | Description                                                                                                                                                                                                                                  |
|----------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Params](/docs/k6/latest/javascript-api/k6-websockets/params/)                                                                   | Used for setting various WebSocket connection parameters such as headers, cookie jar, compression, etc.                                                                                                                                      |
| [WebSocket(url, protocols, params)](/docs/k6/latest/javascript-api/k6-websockets/websocket/)                                     | Constructs a new WebSocket connection.                                                                                                                                                                                                       |
| [WebSocket.close()](/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-close/)                                     | Close the WebSocket connection.                                                                                                                                                                                                              |
| [WebSocket.ping()](/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-ping/)                                       | Send a ping.                                                                                                                                                                                                                                 |
| [WebSocket.send(data)](/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-send/)                                   | Send data.                                                                                                                                                                                                                                   |
| [WebSocket.addEventListener(event, handler)](/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-addeventlistener/) | Add an event listener on the connection for specific event.                                                                                                                                                                                  |
| [Blob](/docs/k6/latest/javascript-api/k6-websockets/blob/)                                                                       | Interface that represents a blob, which is a file-like object of immutable, raw data; they can be read as text or binary data, or converted into a [ReadableStream](/docs/k6/latest/javascript-api/k6-experimental/streams/readablestream/). |

## k6/ws

The [`k6/ws` module](/docs/k6/latest/javascript-api/k6-ws/) provides a [WebSocket](https://en.wikipedia.org/wiki/WebSocket) client implementing the [WebSocket protocol](http://www.rfc-editor.org/rfc/rfc6455.txt).

Expand table

| Function                                                                          | Description                                                                                                                                                                                                       |
|-----------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [connect( url, params, callback )](/docs/k6/latest/javascript-api/k6-ws/connect/) | Create a WebSocket connection, and provides a [Socket](/docs/k6/latest/javascript-api/k6-ws/socket/) client to interact with the service. The method blocks the test finalization until the connection is closed. |

Expand table

| Class/Method                                                                                              | Description                                                                                                                                                                                         |
|-----------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Params](/docs/k6/latest/javascript-api/k6-ws/params/)                                                    | Used for setting various WebSocket connection parameters such as headers, cookie jar, compression, etc.                                                                                             |
| [Socket](/docs/k6/latest/javascript-api/k6-ws/socket/)                                                    | WebSocket client used to interact with a WS connection.                                                                                                                                             |
| [Socket.close()](/docs/k6/latest/javascript-api/k6-ws/socket/socket-close/)                               | Close the WebSocket connection.                                                                                                                                                                     |
| [Socket.on(event, callback)](/docs/k6/latest/javascript-api/k6-ws/socket/socket-on/)                      | Set up an event listener on the connection for any of the following events:  <br />\- open  <br />\- binaryMessage  <br />\- message  <br />\- ping  <br />\- pong  <br />\- close  <br />\- error. |
| [Socket.ping()](/docs/k6/latest/javascript-api/k6-ws/socket/socket-ping/)                                 | Send a ping.                                                                                                                                                                                        |
| [Socket.send(data)](/docs/k6/latest/javascript-api/k6-ws/socket/socket-send/)                             | Send string data.                                                                                                                                                                                   |
| [Socket.sendBinary(data)](/docs/k6/latest/javascript-api/k6-ws/socket/socket-sendbinary/)                 | Send binary data.                                                                                                                                                                                   |
| [Socket.setInterval(callback, interval)](/docs/k6/latest/javascript-api/k6-ws/socket/socket-setinterval/) | Call a function repeatedly at certain intervals, while the connection is open.                                                                                                                      |
| [Socket.setTimeout(callback, period)](/docs/k6/latest/javascript-api/k6-ws/socket/socket-settimeout/)     | Call a function with a delay, if the connection is open.                                                                                                                                            |

## crypto

The [`crypto` module](/docs/k6/latest/javascript-api/crypto/) provides a WebCrypto API implementation.

Expand table

| Class/Method                                                              | Description                                                                                                                                                                                |
|---------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [getRandomValues](/docs/k6/latest/javascript-api/crypto/getrandomvalues/) | Fills the passed `TypedArray` with cryptographically sound random values.                                                                                                                  |
| [randomUUID](/docs/k6/latest/javascript-api/crypto/randomuuid/)           | Returns a randomly generated, 36 character long v4 UUID.                                                                                                                                   |
| [subtle](/docs/k6/latest/javascript-api/crypto/subtlecrypto/)             | The [SubtleCrypto](/docs/k6/latest/javascript-api/crypto/subtlecrypto/) interface provides access to common cryptographic primitives, such as hashing, signing, encryption, or decryption. |

> Note
> 
> The `crypto` object is available globally, so you can use it in your script without including an import statement.

## Error codes

The following specific error codes are currently defined:

- 1000: A generic error that isn’t any of the ones listed below.
- 1010: A non-TCP network error - this is a place holder there is no error currently known to trigger it.
- 1020: An invalid URL was specified.
- 1050: The HTTP request has timed out.
- 1100: A generic DNS error that isn’t any of the ones listed below.
- 1101: No IP for the provided host was found.
- 1110: Blacklisted IP was resolved or a connection to such was tried to be established.
- 1111: Blacklisted hostname using The [Block Hostnames](/docs/k6/latest/using-k6/k6-options/reference/#block-hostnames) option.
- 1200: A generic TCP error that isn’t any of the ones listed below.
- 1201: A “broken pipe” on write - the other side has likely closed the connection.
- 1202: An unknown TCP error - We got an error that we don’t recognize but it is from the operating system and has `errno` set on it. The message in `error` includes the operation(write,read) and the errno, the OS, and the original message of the error.
- 1210: General TCP dial error.
- 1211: Dial timeout error - the timeout for the dial was reached.
- 1212: Dial connection refused - the connection was refused by the other party on dial.
- 1213: Dial unknown error.
- 1220: Reset by peer - the connection was reset by the other party, most likely a server.
- 1300: General TLS error
- 1310: Unknown authority - the certificate issuer is unknown.
- 1311: The certificate doesn’t match the hostname.
- 1400 to 1499: error codes that correspond to the [HTTP 4xx status codes for client errors](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_errors)
- 1500 to 1599: error codes that correspond to the [HTTP 5xx status codes for server errors](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#5xx_Server_errors)
- 1600: A generic HTTP/2 error that isn’t any of the ones listed below.
- 1610: A general HTTP/2 GoAway error.
- 1611 to 1629: HTTP/2 GoAway errors with the value of the specific [HTTP/2 error code](https://tools.ietf.org/html/rfc7540#section-7) added to 1611.
- 1630: A general HTTP/2 stream error.
- 1631 to 1649: HTTP/2 stream errors with the value of the specific [HTTP/2 error code](https://tools.ietf.org/html/rfc7540#section-7) added to 1631.
- 1650: A general HTTP/2 connection error.
- 1651 to 1669: HTTP/2 connection errors with the value of the specific [HTTP/2 error code](https://tools.ietf.org/html/rfc7540#section-7) added to 1651.
- 1701: Decompression error.

Read more about [Error codes](/docs/k6/latest/javascript-api/error-codes/).

## jslib

[jslib](/docs/k6/latest/javascript-api/jslib/) is a collection of JavaScript libraries maintained by the k6 team that can be used in k6 scripts.

Expand table

| Library                                                                                                | Description                                                                                   |
|--------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------|
| [aws](/docs/k6/latest/javascript-api/jslib/aws/)                                                       | Library allowing to interact with Amazon AWS services                                         |
| [httpx](/docs/k6/latest/javascript-api/jslib/httpx/)                                                   | Wrapper around [k6/http](/docs/k6/latest/javascript-api/#k6http) to simplify session handling |
| [k6chaijs](/docs/k6/latest/javascript-api/jslib/k6chaijs/)                                             | BDD assertion style                                                                           |
| [http-instrumentation-pyroscope](/docs/k6/latest/javascript-api/jslib/http-instrumentation-pyroscope/) | Library to instrument k6/http to send baggage headers for pyroscope to read back              |
| [http-instrumentation-tempo](/docs/k6/latest/javascript-api/jslib/http-instrumentation-tempo/)         | Library to instrument k6/http to send tracing data                                            |
| [testing](/docs/k6/latest/javascript-api/jslib/testing/)                                               | Advanced assertion library with Playwright-inspired API for protocol and browser testing      |
| [totp](/docs/k6/latest/javascript-api/jslib/totp/)                                                     | TOTP (Time-based One-Time Password) generation and verification                               |
| [utils](/docs/k6/latest/javascript-api/jslib/utils/)                                                   | Small utility functions useful in every day load testing                                      |
