Code Beautifier

Code Beautifier API

Development

Format and beautify your code, JSON, XML, and other formats with customizable options.

Authentication

All API requests require a valid API key passed in the Authorization header as a Bearer token.

Rate Limit

120 requests per minute

Endpoints

1 endpoint available

Overview

Format code with Prettier. Send the code and an optional language and options, and get the formatted code back.

Languages

  • auto (default): picks a parser from the content. Text starting with { or [ is JSON, text containing both < and > is HTML, text containing function, var, let or const is JavaScript, text containing import or export is TypeScript, and anything else is JavaScript.
  • json, html, xml (formatted with the HTML parser), javascript, typescript, css, markdown, yaml

Notes

  • Code is limited to 100,000 characters.
  • Every key in options is optional and takes the Prettier setting of the same name.
  • Invalid JSON returns a 400 with the parse error. For other languages, if the chosen parser fails the code is retried once with the JavaScript parser, as on the web page. usedFallback tells you when that happened and parser names the parser that produced the output. If that also fails you get a 400.
  • The code is formatted only. It is not run.

Endpoints

POST
https://opentools.ca/api/v1/tools/code-beautifier
Format code with Prettier

Request Body

Content-Type: application/json

ParameterTypeRequiredDescription
code
string
Required
The code to format, 1-100,000 characters
language
string
Enum: auto, json, html, xml, javascript, typescript, css, markdown, yaml
OptionalLanguage of the codeDefault: auto
options
object
OptionalFormatting options. All keys are optional.
options.printWidth
number
OptionalLine width to wrap at, 20-500Default: 80
options.tabWidth
number
OptionalSpaces per indentation level, 1-16Default: 2
options.useTabs
boolean
OptionalIndent with tabsDefault: false
options.semi
boolean
OptionalAdd semicolons (JavaScript and TypeScript)Default: true
options.singleQuote
boolean
OptionalUse single quotes instead of double quotesDefault: false
options.trailingComma
string
Enum: none, es5, all
OptionalTrailing commasDefault: es5
options.bracketSpacing
boolean
OptionalSpaces inside object bracesDefault: true
options.arrowParens
string
Enum: avoid, always
OptionalParentheses around a single arrow function parameterDefault: avoid
options.proseWrap
string
Enum: always, never, preserve
OptionalHow to wrap prose (Markdown)Default: preserve
options.htmlWhitespaceSensitivity
string
Enum: css, strict, ignore
OptionalWhitespace sensitivity (HTML)Default: css

Response Example

{
  "success": true,
  "output": "{ \"name\": \"OpenTools\", \"tools\": [1, 2, 3] }\n",
  "language": "auto",
  "parser": "json",
  "usedFallback": false
}

Error Codes

400
Bad Request - Invalid options, invalid JSON, or code that cannot be parsed
401
Unauthorized - Invalid or missing API key
403
Forbidden - API key is inactive or expired, or the request IP is not allowed
429
Too Many Requests - Rate limit exceeded
500
Internal Server Error

Code Examples

curl -X POST https://opentools.ca/api/v1/tools/code-beautifier \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "code": "const a={b:1,c:[1,2,3]};function f(x){return x*2}",
    "language": "javascript",
    "options": { "singleQuote": true, "tabWidth": 4 }
  }'

Ready to get started?

Create an API key to start using the Code Beautifier API.

Rather use it in your browser? Open the Code Beautifier