Getting Started with K6 for API Performance Testing
What is K6?
K6 is an open-source load testing tool developed by Grafana Labs. It allows you to write performance test scripts in JavaScript, which makes it approachable for developers familiar with web technologies. K6 is designed to be lightweight, efficient, and extensible, making it a great choice for testing APIs, microservices, and web applications.
Key Features:
- Script tests in JavaScript (ES6)
- CLI-based and integrates easily with CI/CD pipelines
- Can simulate thousands of virtual users (VUs)
- Generates detailed metrics and reports
- Supports cloud execution with K6 Cloud (paid)
Environment Setup: Installing K6 on Windows
Step 1: Open PowerShell or Command Prompt as Administrator
- Press Start.
- Type PowerShell (or cmd).
- Right-click on it.
- Click “Run as administrator”.
You’ll see Administrator: Windows PowerShell in the window title—this confirms it’s elevated.
Step 2: Run the Chocolatey Install Script
Set-ExecutionPolicy Bypass -Scope Process -Force; `
[System.Net.ServicePointManager]::SecurityProtocol = `
[System.Net.ServicePointManager]::SecurityProtocol -bor 3072; `
iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

Step 3: Install K6
choco install k6

Step 4: Verify the Installation
k6 version

Writing Your First K6 Script
You can use any code editor to write your K6 test script. Recommended editors:
- Visual Studio Code
- Sublime Text
- Atom
Save your file as test-api.js.
Example: User Management API Test (Using https://reqres.in)

Script Header & Options
import http from 'k6/http';
import { check, sleep } from 'k6';
export let options = {
vus: 5,
duration: '10s',
};
import http
: Imports HTTP module to send requests.check
: Validates HTTP responses.sleep
: Adds delays between virtual user actions.vus: 5
: Simulates 5 virtual users.duration: '10s'
: Runs the test for 10 seconds.
GET Request: Fetching a User
let getRes = http.get('https://reqres.in/api/users/5');
check(getRes, {
'GET status is 200': (r) => r.status === 200,
'GET body contains "charles"': (r) => r.body.includes('charles'),
});
POST Request: Creating a New User
let payloadPost = JSON.stringify({
name: 'Anjali',
job: 'Software Tester',
});
let postRes = http.post('https://reqres.in/api/users', payloadPost, {
headers: { 'Content-Type': 'application/json' },
});
check(postRes, {
'POST status is 201': (r) => r.status === 201,
'POST response has id': (r) => JSON.parse(r.body).id !== undefined,
});
PUT Request: Updating User Info
let payloadPut = JSON.stringify({
name: 'Anjali',
job: 'Team Lead',
});
let putRes = http.put('https://reqres.in/api/users/3', payloadPut, {
headers: { 'Content-Type': 'application/json' },
});
check(putRes, {
'PUT status is 200': (r) => r.status === 200,
'PUT response has updatedAt': (r) => JSON.parse(r.body).updatedAt !== undefined,
});
DELETE Request: Removing a User
let delRes = http.del('https://reqres.in/api/users/3');
check(delRes, {
'DELETE status is 204': (r) => r.status === 204,
});
Final Step: Adding sleep
sleep(1);
Purpose: Simulates a 1-second delay to mimic real-user think time.
Summary
Action | API Used | Key Purpose | Expected Status |
---|---|---|---|
GET | /api/users/5 | Fetch a user | 200 |
POST | /api/users | Create new user | 201 |
PUT | /api/users/3 | Update existing user info | 200 |
DELETE | /api/users/3 | Remove a user | 204 |
Exporting Results (Optional)
k6 run --out json=result.json test-api.js
Wrapping Up
Performance testing doesn’t have to be complicated—and K6 proves that. Through this blog, we’ve taken a practical journey into what makes K6 a standout tool for testing APIs under load.
- The fundamentals of K6 and how to get it up and running on Windows
- Building real-world test scenarios using HTTP methods like GET, POST, PUT, and DELETE
- Writing readable, maintainable scripts in JavaScript
- Validating performance metrics and response accuracy
- Viewing results and planning for scale
K6 strikes a fine balance between simplicity and capability. Its lightweight architecture and developer-first scripting model make it an excellent fit for teams embracing automation and CI/CD pipelines.