| name | curl-command-generator |
| description | Generates ready-to-run cURL commands from Express, Next.js, Fastify, or other API routes. Creates copy-paste commands with proper headers, authentication, and request bodies. Use when users request "generate curl commands", "curl examples", "api curl", or "command line api testing". |
cURL Command Generator
Generate ready-to-run cURL commands for quick API testing from the command line.
Core Workflow
- Scan routes: Find all API route definitions
- Extract metadata: Methods, paths, params, bodies
- Generate commands: Create cURL commands with flags
- Add authentication: Bearer, Basic, API Key headers
- Include examples: Request bodies with sample data
- Output options: Markdown, shell script, or plain text
Basic cURL Syntax
curl -X GET "http://localhost:3000/api/users"
curl -X POST "http://localhost:3000/api/users" \
-H "Content-Type: application/json" \
-d '{"name": "John", "email": "john@example.com"}'
curl -X GET "http://localhost:3000/api/users" \
-H "Authorization: Bearer YOUR_TOKEN"
curl -X GET "http://localhost:3000/api/users?page=1&limit=10"
curl -i -X GET "http://localhost:3000/api/users"
curl -v -X GET "http://localhost:3000/api/users"
Generator Script
import * as fs from "fs";
interface RouteInfo {
method: string;
path: string;
name: string;
description?: string;
body?: object;
queryParams?: { name: string; value: string }[];
auth?: boolean;
}
interface CurlOptions {
baseUrl: string;
authHeader?: string;
verbose?: boolean;
showHeaders?: boolean;
format?: "markdown" | "shell" | "plain";
}
function generateCurlCommand(route: RouteInfo, options: CurlOptions): string {
const parts: string[] = ["curl"];
if (options.verbose) {
parts.push("-v");
}
(options.) {
parts.();
}
parts.();
url = ;
url = url.(, );
(route.?.) {
queryString = route.
.( )
.();
url += ;
}
parts.();
([, , ].(route.)) {
parts.();
}
(route. && options.) {
parts.();
}
(route. && [, , ].(route.)) {
bodyJson = .(route.);
parts.();
}
parts.();
}
(): {
: [] = [];
(options. === ) {
lines.();
lines.();
lines.();
lines.();
} (options. === ) {
lines.();
lines.();
lines.();
lines.();
lines.();
}
groupedRoutes = (routes);
( [resource, resourceRoutes] .(groupedRoutes)) {
(options. === ) {
lines.();
lines.();
} (options. === ) {
lines.();
lines.();
}
( route resourceRoutes) {
(options. === ) {
lines.();
(route.) {
lines.(route.);
}
lines.();
lines.();
} {
lines.();
}
lines.((route, options));
(options. === ) {
lines.();
}
lines.();
}
}
lines.();
}
(): <, []> {
: <, []> = {};
( route routes) {
parts = route..().();
resource = parts[] || ;
(!groups[resource]) {
groups[resource] = [];
}
groups[resource].(route);
}
groups;
}
(): {
str.().() + str.();
}
Complete Example Output (Markdown)
# API cURL Commands
Base URL: `http://localhost:3000/api`
## Authentication
### Login
```bash
curl -X POST "http://localhost:3000/api/auth/login" \
-H "Content-Type: application/json" \
-d '{"email": "user@example.com", "password": "password123"}'
Register
curl -X POST "http://localhost:3000/api/auth/register" \
-H "Content-Type: application/json" \
-d '{"name": "New User", "email": "new@example.com", "password": "securepass123"}'
Users
List Users
curl -X GET "http://localhost:3000/api/users?page=1&limit=10" \
-H "Authorization: Bearer YOUR_TOKEN"
Get User by ID
curl -X GET "http://localhost:3000/api/users/{id}" \
-H "Authorization: Bearer YOUR_TOKEN"
Create User
curl -X POST "http://localhost:3000/api/users" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_TOKEN" \
-d '{"name": "John Doe", "email": "john@example.com", "role": "user"}'
Update User
curl -X PUT "http://localhost:3000/api/users/{id}" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_TOKEN" \
-d '{"name": "John Updated", "email": "john.updated@example.com"}'
Delete User
curl -X DELETE "http://localhost:3000/api/users/{id}" \
-H "Authorization: Bearer YOUR_TOKEN"
## Shell Script Output
```bash
#!/bin/bash
# api-commands.sh
BASE_URL="${BASE_URL:-http://localhost:3000/api}"
AUTH_TOKEN="${AUTH_TOKEN:-your-token-here}"
# Authentication
# Login
login() {
curl -X POST "${BASE_URL}/auth/login" \
-H "Content-Type: application/json" \
-d "{\"email\": \"$1\", \"password\": \"$2\"}"
}
# Users
# List Users
list_users() {
local page="${1:-1}"
local limit="${2:-10}"
curl -X GET "${BASE_URL}/users?page=${page}&limit=${limit}" \
-H "Authorization: Bearer ${AUTH_TOKEN}"
}
# Get User by ID
get_user() {
curl -X GET "${BASE_URL}/users/$1" \
-H "Authorization: Bearer ${AUTH_TOKEN}"
}
# Create User
create_user() {
curl -X POST "${BASE_URL}/users" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${AUTH_TOKEN}" \
-d "$1"
}
# Update User
update_user() {
curl -X PUT "${BASE_URL}/users/$1" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${AUTH_TOKEN}" \
-d "$2"
}
# Delete User
delete_user() {
curl -X DELETE "${BASE_URL}/users/$1" \
-H "Authorization: Bearer ${AUTH_TOKEN}"
}
# Usage examples:
# ./api-commands.sh
# login user@example.com password123
# list_users 1 10
# get_user abc123
# create_user '{"name": "John", "email": "john@example.com"}'
# update_user abc123 '{"name": "John Updated"}'
# delete_user abc123
# Execute command if provided
if [ -n "$1" ]; then
"$@"
fi
Advanced cURL Flags
curl -X GET "http://localhost:3000/api/users" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer TOKEN" \
-i
-v
-s
-S
-o response.json
-w "\n%{http_code}\n"
--connect-timeout 5
--max-time 30
-L
-k
curl -s "http://localhost:3000/api/users" | jq .
curl -c cookies.txt -b cookies.txt "http://localhost:3000/api/auth/login"
curl -X POST "http://localhost:3000/api/upload" \
-H "Authorization: Bearer TOKEN" \
-F "file=@./document.pdf"
curl -X POST "http://localhost:3000/api/form" \
-d "name=John&email=john@example.com"
curl -X GET "http://localhost:3000/api/users" \
-w "\nDNS: %{time_namelookup}s\nConnect: %{time_connect}s\nTotal: %{time_total}s\n"
Environment-Specific Commands
DEV_URL="http://localhost:3000/api"
DEV_TOKEN=""
STAGING_URL="https://staging-api.example.com"
STAGING_TOKEN=""
PROD_URL="https://api.example.com"
PROD_TOKEN=""
#!/bin/bash
set -a
source .env.curl
set +a
ENV="${1:-dev}"
case $ENV in
dev)
BASE_URL="$DEV_URL"
AUTH_TOKEN="$DEV_TOKEN"
;;
staging)
BASE_URL="$STAGING_URL"
AUTH_TOKEN="$STAGING_TOKEN"
;;
prod)
BASE_URL="$PROD_URL"
AUTH_TOKEN="$PROD_TOKEN"
;;
esac
export BASE_URL AUTH_TOKEN
echo "Using $ENV environment: $BASE_URL"
CLI Script
#!/usr/bin/env node
import * as fs from "fs";
import { program } from "commander";
program
.name("curl-gen")
.description("Generate cURL commands from API routes")
.option("-f, --framework <type>", "Framework type", "express")
.option("-s, --source <path>", "Source directory", "./src")
.option("-o, --output <path>", "Output file", "./docs/api-curl.md")
.option("-b, --base-url <url>", "Base URL", "http://localhost:3000/api")
.option("--format <type>", "Output format (markdown|shell|plain)", "markdown")
.option("-v, --verbose", "Include verbose flag")
.parse();
const options = program.opts();
async function main() {
const routes = await scanRoutes(options.framework, options.source);
const content = (routes, {
: options.,
: ,
: options.,
: options.,
});
fs.(options., content);
.();
}
();
Makefile Integration
BASE_URL ?= http://localhost:3000/api
AUTH_TOKEN ?= your-token-here
.PHONY: api-login api-users api-user api-create-user
api-login:
@curl -X POST "$(BASE_URL)/auth/login" \
-H "Content-Type: application/json" \
-d '{"email": "$(EMAIL)", "password": "$(PASSWORD)"}'
api-users:
@curl -X GET "$(BASE_URL)/users" \
-H "Authorization: Bearer $(AUTH_TOKEN)"
api-user:
@curl -X GET "$(BASE_URL)/users/$(ID)" \
-H "Authorization: Bearer $(AUTH_TOKEN)"
api-create-user:
@curl -X POST "$(BASE_URL)/users" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(AUTH_TOKEN)" \
-d '$(DATA)'
Best Practices
- Use variables: Replace tokens and IDs with placeholders
- Pretty print: Pipe to
jq for readable JSON output
- Save responses: Use
-o to save responses for analysis
- Check status: Use
-w "%{http_code}" to see status codes
- Silent mode: Use
-sS for scripts to hide progress
- Document examples: Include realistic sample data
- Version control: Commit curl docs to repository
- Environment files: Use env files for different environments
Output Checklist