SQL Formatter API: The Complete Guide

Need to format SQL queries consistently? This guide covers everything you need to know about SQL formatting via API, including dialect support, formatting options, and implementation examples in multiple languages.

Why Format SQL?

Well-formatted SQL is easier to read, debug, and maintain. Consistent formatting across a team improves code reviews and reduces errors. An API-based formatter ensures everyone follows the same style without manual effort.

Unformatted SQL like SELECT id,name,email FROM users WHERE status='active' AND created_at>='2024-01-01' ORDER BY name becomes much more readable when properly formatted.

Supported SQL Dialects

The API supports multiple SQL dialects with dialect-specific formatting:

  • Standard SQL: ANSI SQL standard
  • MySQL: MySQL-specific syntax
  • PostgreSQL: PostgreSQL-specific syntax
  • SQLite: SQLite-specific syntax
  • SQL Server: T-SQL formatting
  • Oracle: PL/SQL formatting
Pro Tip: Specify the correct dialect to ensure keywords and functions are properly recognized and formatted.

Using the SQL Formatter API

TinyFn provides a simple endpoint to format SQL queries:

API Request
POST https://api.tinyfn.io/v1/format/sql
Headers: X-API-Key: your-api-key
Content-Type: application/json

{
  "sql": "SELECT id,name,email FROM users WHERE status='active'",
  "dialect": "postgresql"
}
Response
{
  "formatted": "SELECT\n  id,\n  name,\n  email\nFROM\n  users\nWHERE\n  status = 'active'",
  "dialect": "postgresql",
  "keywords_uppercase": true
}

Parameters

Parameter Type Description
sql string The SQL query to format (required)
dialect string SQL dialect: sql, mysql, postgresql, etc. (default: sql)
indent integer Indentation spaces (default: 2)
uppercase boolean Uppercase keywords (default: true)

Code Examples

JavaScript / Node.js

const response = await fetch('https://api.tinyfn.io/v1/format/sql', {
  method: 'POST',
  headers: {
    'X-API-Key': 'your-api-key',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    sql: 'SELECT * FROM users WHERE id=1',
    dialect: 'mysql'
  })
});
const { formatted } = await response.json();
console.log(formatted);

Python

import requests

response = requests.post(
    'https://api.tinyfn.io/v1/format/sql',
    json={'sql': 'SELECT * FROM users WHERE id=1', 'dialect': 'postgresql'},
    headers={'X-API-Key': 'your-api-key'}
)
data = response.json()
print(data['formatted'])

cURL

curl -X POST "https://api.tinyfn.io/v1/format/sql" \
  -H "X-API-Key: your-api-key" \
  -H "Content-Type: application/json" \
  -d '{"sql": "SELECT * FROM users", "dialect": "mysql"}'

Formatting Options

  • Indentation: Configure spaces or tabs for indentation
  • Keyword Case: Uppercase, lowercase, or preserve keywords
  • Line Width: Maximum line width before wrapping
  • Comma Position: Leading or trailing commas
  • Newlines: Configure newlines around clauses

Best Practices

  1. Use in CI/CD: Integrate SQL formatting into your build pipeline
  2. Choose a dialect: Always specify the correct SQL dialect
  3. Standardize settings: Use consistent formatting options across your team
  4. Format before commit: Use pre-commit hooks to format SQL files

Use via MCP

Your AI agent can call this tool directly via Model Context Protocol — no HTTP code needed. Add TinyFn to Claude Desktop, Cursor, or any MCP client:

{
  "mcpServers": {
    "tinyfn-format": {
      "url": "https://api.tinyfn.io/mcp/format/",
      "headers": {
        "X-API-Key": "your-api-key"
      }
    }
  }
}

See all formatting tools available via MCP in our Formatting MCP Tools for AI Agents guide.

Try the SQL Formatter API

Get your free API key and start formatting SQL queries in seconds.

Get Free API Key

Ready to try TinyFn?

Get your free API key and start building in minutes.

Get Free API Key