Code Example Standards
This guide defines the standards for code examples across the Kernel documentation.General Principles
- Context-aware completeness: Full examples must run as-is. Focused snippets can rely on variables introduced by surrounding text or sibling examples, but they must make those dependencies obvious.
- Consistent naming: Use standardized variable names across all examples
- No real secrets: Never include real API keys, passwords, tokens, or live credentials. Use obviously fake values when an auth-flow example needs credential-shaped input.
- Multi-language support: When applicable, show TypeScript/JavaScript, Python, and Go examples
Variable Naming Conventions
TypeScript/JavaScript
- SDK client:
kernel - Browser instance:
kernelBrowser - Additional browsers:
kernelBrowser2,kernelBrowserAuto, etc. - Playwright/Puppeteer browser:
browser - Context:
context - Page:
page
Python
- SDK client:
kernel - Browser instance:
kernel_browser - Additional browsers:
kernel_browser2, etc. - Playwright browser:
browser - Context:
context - Page:
page
Go
- SDK client:
client - Browser instance:
kernelBrowser - Additional browsers:
kernelBrowser2, etc. - Context:
ctx - Session ID:
sessionID - Invocation ID:
invocationID
Code Example Structure
Full examples vs focused snippets
Use a full example when the reader needs to copy and run a standalone program. Include imports, SDK initialization, context setup, the main operation, and error handling. Use a focused snippet when the page is walking through one step in a larger flow. Keep the snippet small, but rely only on variables the page already introduced, such asclient, ctx,
kernelBrowser, auth, or browser.
Minimal Example (Browser Creation)
Always include:- Import statement
- SDK initialization
- The main operation
- Return value or console output (when relevant)
Full Example (With Browser Automation)
For examples showing browser automation, include:- All necessary imports
- SDK initialization
- Browser creation
- CDP connection
- Browser automation code
- Error handling (try/finally)
- Cleanup
SDK Initialization
✅ Correct - No hardcoded credentials
KERNEL_API_KEY environment variable.
❌ Incorrect - Hardcoded credentials
Feature-Specific Examples
Simple Feature Toggle
For simple feature flags (stealth, headless, etc.):Feature with Configuration
For features requiring configuration objects:App Development Examples
Kernel app examples currently use TypeScript/JavaScript and Python. Add a Go version only after the Go SDK has documented app framework support and the snippet has been tested against that SDK. For Kernel app examples, follow this pattern:Common Patterns
Pattern: Context and Page Access
Kernel browsers launch with a default context and page. Always use this pattern:Pattern: Error Handling
Always include proper error handling:Code Formatting
Indentation
- TypeScript/JavaScript: 2 spaces
- Python: 4 spaces
- Go: tabs from
gofmt
String Quotes
- TypeScript/JavaScript: Single quotes
'(except for avoiding escaping) - Python: Double quotes
" - Go: Double quotes
"
Line Length
- Keep lines under 100 characters when possible
- Break long parameter lists across multiple lines
Comments
- Use comments sparingly; keep code self-explanatory
- Add comments only for non-obvious logic or important context
- Never add comments like “NEW CODE:” or similar meta-comments
URL and Placeholder Formatting
URLs with Placeholders
Use angle brackets for placeholders:IDs in Code
Use descriptive strings:Multi-Language CodeGroups
Always use<CodeGroup> with proper language labels:
Checklist
Before publishing a code example, verify:- Includes all necessary imports
- SDK is initialized without hardcoded API keys
- Variable names follow conventions
- Code is complete and runnable, or it is a focused snippet with obvious prerequisites
- Includes error handling (for full examples)
- Includes cleanup code (for full examples)
- Uses proper indentation and formatting
- TypeScript/JavaScript, Python, and Go versions are provided (when applicable)
- Go examples are formatted with
gofmt - Go examples are tested against the actual Go SDK version the docs claim to support
- Code has been tested or follows proven patterns
Reference
See these files for examples:introduction/create.mdx- Standard browser creation patternapps/develop.mdx- App development patternbrowsers/file-io.mdx- Complex automation example