DEV Community

Michael Lip
Michael Lip

Posted on • Originally published at zovo.one

From curl to Production Code: Bridging the API Documentation Gap

API documentation almost universally uses curl for examples. It is the lingua franca of HTTP requests. But no production application makes requests with curl. You need to translate those examples into your application's language, and the gap between a curl example and production-ready code is wider than it appears.

Beyond basic translation

Converting curl -X GET to requests.get() is trivial. The harder part is making the resulting code production-ready:

Error handling: Curl fails silently or prints error messages. Production code needs to handle network errors, timeouts, 4xx responses, 5xx responses, and rate limits differently.

# What the converter generates
response = requests.get('https://api.example.com/data',
    headers={'Authorization': 'Bearer token'})

# What production code needs
try:
    response = requests.get(
        'https://api.example.com/data',
        headers={'Authorization': 'Bearer token'},
        timeout=10
    )
    response.raise_for_status()
    data = response.json()
except requests.exceptions.Timeout:
    logger.warning("API timeout")
    raise
except requests.exceptions.HTTPError as e:
    if e.response.status_code == 429:
        retry_after = int(e.response.headers.get('Retry-After', 60))
        time.sleep(retry_after)
        # retry logic
    else:
        raise
Enter fullscreen mode Exit fullscreen mode

Authentication management: Curl examples hardcode tokens. Production code needs to retrieve tokens from secure storage, handle token refresh, and never log credentials.

Retry logic: APIs fail. Networks are unreliable. Production code needs exponential backoff with jitter for transient failures.

Pagination: The curl example shows one request. The actual data set requires iterating through pages.

Language-specific idioms

A good curl-to-code converter generates idiomatic code for each language, not just syntactic translations:

Python: Use response.json() instead of json.loads(response.text). Use params for query strings instead of building URLs manually. Use json= for JSON bodies instead of data=json.dumps().

JavaScript: Use async/await instead of .then() chains. Use the modern fetch API instead of XMLHttpRequest. Handle AbortController for timeouts.

PHP: Use Guzzle instead of raw curl_* functions for cleaner code. Use array syntax for headers instead of string concatenation.

Go: Use context.WithTimeout for request timeouts. Properly close response bodies with defer resp.Body.Close(). Handle errors at each step.

Common API patterns in curl

OAuth 2.0 Bearer token:

curl -H "Authorization: Bearer eyJhbGciOi..."
Enter fullscreen mode Exit fullscreen mode

API key in header:

curl -H "X-API-Key: abc123"
Enter fullscreen mode Exit fullscreen mode

API key in query parameter:

curl "https://api.example.com/data?api_key=abc123"
Enter fullscreen mode Exit fullscreen mode

Basic authentication:

curl -u username:password
Enter fullscreen mode Exit fullscreen mode

File upload:

curl -F "file=@document.pdf" -F "description=My doc"
Enter fullscreen mode Exit fullscreen mode

Each of these has a specific, idiomatic translation in every target language.

The tool

For converting curl commands to clean, language-appropriate code, I built a curl to code converter that supports multiple languages and generates code that follows each language's conventions and best practices.


I'm Michael Lip. I build free developer tools at zovo.one. 500+ tools, all private, all free.

Top comments (0)