k6 Cloud Output Authentication Error Fix
In this tutorial, you'll learn about k6 cloud output authentication error fix. We cover key concepts, practical examples, and best practices.
Your k6 test fails with could not authenticate to k6 Cloud when using --out cloud — the API token is missing, expired, or the project ID is incorrect. The k6 Cloud output requires valid credentials and network access to api.k6.io.
The Problem
k6 run --out cloud script.js
ERRO[0000] failed to authenticate with k6 Cloud:
could not authenticate to k6 Cloud: invalid token
The K6_CLOUD_TOKEN environment variable is not set or contains an invalid or expired token.
Step-by-Step Fix
1. Set the cloud token
# Set token from k6 Cloud dashboard
export K6_CLOUD_TOKEN="your_api_token_here"
# Or pass inline
k6 run --out cloud -e K6_CLOUD_TOKEN="your_token" script.js
2. Verify the token
# Test the token
curl -H "Authorization: Token your_token" https://api.k6.io/v1/me
# Expected output: {"email": "you@example.com", "name": "Your Name"}
3. Set project ID (for organization environments)
# Find your project ID in k6 Cloud dashboard
export K6_CLOUD_PROJECT_ID="12345"
# Or pass inline
k6 run --out cloud -e K6_CLOUD_PROJECT_ID="12345" script.js
4. Configure test run details
import http from 'k6/http';
export const options = {
ext: {
loadimpact: {
name: 'Production Smoke Test',
projectID: parseInt(__ENV.K6_CLOUD_PROJECT_ID) || 0,
distribution: {
'amazon:us-east-1': { loadZone: 'amazon:us-east-1', percent: 50 },
'amazon:eu-west-1': { loadZone: 'amazon:eu-west-1', percent: 50 },
},
},
},
thresholds: {
http_req_duration: ['p(95)<2000'],
},
};
export default function () {
http.get('https://api.example.com/health');
}
5. Handle network/proxy issues
# Check network connectivity
curl -I https://api.k6.io/v1/me
# Use a proxy if needed
export HTTP_PROXY=http://proxy.company.com:8080
export HTTPS_PROXY=http://proxy.company.com:8080
Expected output:
✓ Test run started in k6 Cloud
✓ Run ID: 123456
✓ View results at: https://app.k6.io/runs/123456
Prevention Tips
- Set
K6_CLOUD_TOKENin your CI/CD environment secrets - Use
K6_CLOUD_PROJECT_IDfor multi-project organizations - Use the
ext.loadimpactoption to name and configure test runs - Verify network access to
api.k6.iobefore running - Test with
--out cloudon a short script first
Common Mistakes with cloud output
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging
These mistakes appear frequently in real-world K6 code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro