{"id":27230467,"url":"https://github.com/grafana/openapi-to-k6/","last_synced_at":"2025-04-10T13:04:21.668Z","repository":{"id":261689119,"uuid":"868815964","full_name":"grafana/openapi-to-k6","owner":"grafana","description":"A CLI tool to convert an OpenAPI schema to a TypeScript client to use with k6. ","archived":false,"fork":false,"pushed_at":"2025-02-04T09:14:02.000Z","size":958,"stargazers_count":17,"open_issues_count":4,"forks_count":1,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-28T03:23:13.643Z","etag":null,"topics":["api-development","api-testing","code-generation","devops","k6","load-testing","openapi","performance-testing","test-automation","testing-tools"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/@grafana/openapi-to-k6","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"agpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/grafana.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-07T08:32:10.000Z","updated_at":"2025-03-23T13:33:15.000Z","dependencies_parsed_at":"2024-11-07T22:32:50.704Z","dependency_job_id":"3a7e7045-cce7-44a5-983d-a007ddaa354e","html_url":"https://github.com/grafana/openapi-to-k6","commit_stats":null,"previous_names":["grafana/openapi-to-k6"],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grafana%2Fopenapi-to-k6","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grafana%2Fopenapi-to-k6/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grafana%2Fopenapi-to-k6/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grafana%2Fopenapi-to-k6/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/grafana","download_url":"https://codeload.github.com/grafana/openapi-to-k6/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248223825,"owners_count":21068069,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api-development","api-testing","code-generation","devops","k6","load-testing","openapi","performance-testing","test-automation","testing-tools"],"created_at":"2025-04-10T13:01:34.361Z","updated_at":"2025-04-10T13:04:21.650Z","avatar_url":"https://github.com/grafana.png","language":"TypeScript","funding_links":[],"categories":["Articles"],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\n  \u003cimg\n    src=\"https://raw.githubusercontent.com/grafana/openapi-to-k6/main/images/openapi-to-k6.png\"\n    width=\"600\"\n    style=\"pointer-events: none;\" /\u003e\n  \u003cbr /\u003e\n\u003c/div\u003e\n\n## Overview\n\nThe _openapi-to-k6_ repository is a tool designed to ease the process of writing k6 scripts.\nIt generates a TypeScript client from OpenAPI specification which can be imported in your k6 script to\neasily call your endpoints and have auto completion in your IDE.\n\nThis allows developers to easily create performance tests for their APIs based on their existing\nOpenAPI documentation.\n\nAlong with the client, it also generates a sample k6 script as an example of how to use the client.\n\nThe generated client exports a class with methods for each endpoint in the OpenAPI specification. You can create\na instance of the class and use the methods to call the endpoints.\n\nTo take a look at a few examples of how the generated client looks and sample script looks, check out the [examples](./examples) directory.\n\n\n## Getting started\n\n1. Install the tool globally via\n\n    ```shell\n    npm install -g @grafana/openapi-to-k6\n    ```\n\n2. To start using the tool either give path to your OpenAPI schema file or provide a URL to your Open\n   API schema and the output path where you want to generate the client files.\n\n    ```shell\n    openapi-to-k6 \u003cpath-to-openapi-schema | url-to-openapi-schema\u003e \u003coutput path\u003e\n    ```\n\n    This will the generate a TypeScript client and a sample k6 script in the corresponding directory.\n\n    You can also supply the optional flag `--include-sample-script` to also generate a sample k6 script\n    along with the client.\n\n💡 _Note_: The tool supports both JSON and YAML format for OpenAPI schema.\n\n### Options\n\nFollowing are some of the configuration options supported by the tool.\n\n1. `--mode` or `-m`: Specify the mode to use for generating the client. Following are available options:\n   1. `single`: This is the default mode used is nothing is specified. It generated the TypeScript client as a single file with all the types and implementation in a single file.\n   2. `split`: This mode splits the types and implementation into separate files.\n   3. `tags`: This modes splits your OpenAPI schema based on the tags and generates a separate client for each tag. If a route has no tag set, it will be available in `default.ts` file.\n\n   To check how the output looks for each mode, check out the [examples](./examples) directory.\n2. `--only-tags`: Filter the generated client to only include routes with specific tags from your OpenAPI schema. Multiple tags can be specified to include routes matching any of those tags. Routes without tags will be excluded. This is useful for generating focused clients that only contain the endpoints you need.\ne.g. `openapi-to-k6 \u003cpath-to-openapi-schema\u003e \u003coutput path\u003e --only-tags ItemsHeader` will generate a client with only the routes that have the `ItemsHeader` tag. Multiple tags can be specified by using multiple `--only-tags` flags or by separating them with spaces.\n3. `--disable-analytics`: Disable anonymous usage analytics reporting which helping making the tool better. You can also set an environment variable `DISABLE_ANALYTICS=true` to disable the analytics.\n4. `--include-sample-script`: Generate a sample k6 script. The generated sample script uses the examples defined in the OpenAPI schema requests to make the script usable out of the box. If the examples are not defined, it will use Faker to generate random data.\n5. `--verbose` or `-v` : Enable verbose logging to see more detailed logging output.\n6. `--help` or `-h` : Show help message.\n\n## Developing locally\n\n1. Clone the repository\n\n```shell\ngit clone https://github.com/grafana/openapi-to-k6\n```\n\n2. Install dependencies\n\n```shell\nnpm install\n```\n\n3. Run the sdk generator from source\n\n```shell\nnpm run dev \u003cpath-to-openapi-schema\u003e \u003coutput path\u003e\n```\n\nThis will generate the SDK files in the corresponding directory.\n\n4. Import them in you k6 script and run the script using the following command\n\n```shell\nk6 run --compatibility-mode=experimental_enhanced \u003cpath-to-k6-script\u003e.ts\n```\n\nNote: `--compatibility-mode` is needed to use a typescript file as K6 script. To know more about it, click [here](https://grafana.com/docs/k6/latest/using-k6/javascript-typescript-compatibility-mode/).\n\n## Running E2E tests\n\nWe have some end-to-end tests to ensure the generated SDK works as expected. To run these tests, you can use the following command:\n\n1. Navigate to the test directory\n\n```shell\ncd tests/e2e/\n```\n\n2. Use Mockoon CLI to start the mock server which will create a mock server for the endpoints defined in the OpenAPI specification.\nThis will run the mock server in a docker container in background.\n\n```shell\ndocker run -v ./schema.json:/tmp/schema.json -p 3000:3000 mockoon/cli:latest -d /tmp/schema.json\n```\n\n3. Assuming you have already followed previous steps and have the environment set up, you can generate the SDK by using\n\n```shell\nnpm run dev -- ./schema.json ./sdk.ts\n```\n\n4. Run the K6 script\n\n```shell\nk6 run --compatibility-mode=experimental_enhanced ./K6Script.ts\n```\n\n## Packaging\n\n1. Run the command `npm run build` to package the project together for distribution.\n2. Install the compiled package locally by using `npm install .` or `npm install -g .`.\n3. Use the CLI `k6-sdkgen \u003cpath-to-openapi-schema\u003e \u003coutput path\u003e`\n\n## Releasing\n\nTo release a new version of the tool, create a new release on GitHub with the new version number as tag (e.g. `0.1.0` ) and the release notes. After the release is created, the GitHub actions will automatically package the tool and publish it to npm.\n\nSpecial mention for the the open-source library [Orval](https://orval.dev/) which is used for the generation of the TypeScript client.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrafana%2Fopenapi-to-k6%2F","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgrafana%2Fopenapi-to-k6%2F","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrafana%2Fopenapi-to-k6%2F/lists"}