Code Generator – Export Requests as Code Snippets
- Dominant language
- TypeScript
- Stars
- 49
- Forks
- 25
- Avg merge
- 3d 21h
- Merged PRs (30d)
- 4
Description
> ℹ️ _This issue was partially AI-generated and may contain technical inaccuracies._
## Summary
Add a code generator that converts any request into a ready-to-use code snippet in common languages and tools. This removes the need to manually re-create a request outside of Trufos when integrating it into a codebase or sharing it with others.
## Is your feature request related to a problem?
When a developer has built and tested a request in Trufos, they often need to replicate it in actual code (e.g. in a backend service, a test suite, or a script). Currently there is no way to do this from within the app — they have to manually
translate the URL, headers, auth, and body by hand, which is tedious and error-prone.
## Describe the solution you'd like
### UI
- A **"Generate Code"** button or icon in the request top bar (next to the Send button) or accessible via right-click on a request
- Opens a modal or side panel with:
- A **language/tool selector** dropdown
- A **read-only code editor** showing the generated snippet (with syntax highlighting)
- A **Copy to clipboard** button
### Supported targets (priority order)
| Target | Example |
|---|---|
| `curl` | `curl -X POST https://... -H "..."` |
| JavaScript `fetch` | `fetch('https://...', { method: 'POST', ... })` |
| JavaScript `axios` | `axios.post('https://...', { ... })` |
| Python `requests` | `import requests; requests.post(...)` |
| TypeScript `fetch` | Same as JS fetch with types |
### Snippet content
The generated code must include all active request properties:
- HTTP method and URL (with query params)
- Headers (only enabled ones)
- Body (raw text, JSON, form-data, or file reference)
- Authorization (e.g. rendered as a Bearer header or Basic auth header)
Variables (e.g. `{{baseUrl}}`) should be rendered with their current resolved values in the snippet, or kept as named constants at the top of the snippet.
## Describe alternatives you've considered
- Copying the URL and headers manually — error-prone, especially with auth and body
- Using browser DevTools to capture the request — requires actually sending it first and leaves the Trufos workflow
## Additional context
- Bruno has a built-in code generator; Postman has had this feature for years
- The [httpsnippet](https://github.com/Kong/httpsnippet) library supports many targets and could be used as the generation backend to avoid building each target from scratch
## Acceptance Criteria
- [ ] A "Generate Code" action is accessible from the request view
- [ ] At least `curl`, `fetch` (JS), and Python `requests` are supported
- [ ] The snippet includes method, URL, query params, active headers, body, and auth
- [ ] Variables are resolved to their current values in the output
- [ ] The snippet can be copied to clipboard with one click
- [ ] The language selector persists the last used choice
---
**⚠️ Technical/security correction:** The text above asks to render variables (including auth/secret values) as resolved plaintext in the snippet. Since snippets are meant for sharing, this would leak live secrets (Basic base64, bearer/OAuth tokens, secret env vars). Default to keeping `{{variables}}` as unresolved placeholders / named constants, mask secret-typed variables, and make inlining resolved secrets an explicit opt-in with a clear warning. (Same issue flagged in PR #888.)
Contributor guide
Research direction
Start at the request view and its top bar, where the Generate Code action is proposed, and trace how request method, URL, headers, body, authorization, and variables are represented. Compare whether httpsnippet can cover curl, JavaScript fetch, and Python requests, while preserving placeholders and masking secrets by default. Done means the action, selector, clipboard flow, supported snippets, persistence, and security correction meet the listed acceptance criteria.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- electron, javascript, python, react, typescript
- Domain
- api, desktop, frontend, security
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100