
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
optionsis 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.
usedFallbacktells you when that happened andparsernames 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-beautifierFormat code with Prettier
Request Body
Content-Type: application/json
| Parameter | Type | Required | Description |
|---|---|---|---|
| code | string | Required | The code to format, 1-100,000 characters |
| language | string Enum: auto, json, html, xml, javascript, typescript, css, markdown, yaml | Optional | Language of the codeDefault: auto |
| options | object | Optional | Formatting options. All keys are optional. |
| options.printWidth | number | Optional | Line width to wrap at, 20-500Default: 80 |
| options.tabWidth | number | Optional | Spaces per indentation level, 1-16Default: 2 |
| options.useTabs | boolean | Optional | Indent with tabsDefault: false |
| options.semi | boolean | Optional | Add semicolons (JavaScript and TypeScript)Default: true |
| options.singleQuote | boolean | Optional | Use single quotes instead of double quotesDefault: false |
| options.trailingComma | string Enum: none, es5, all | Optional | Trailing commasDefault: es5 |
| options.bracketSpacing | boolean | Optional | Spaces inside object bracesDefault: true |
| options.arrowParens | string Enum: avoid, always | Optional | Parentheses around a single arrow function parameterDefault: avoid |
| options.proseWrap | string Enum: always, never, preserve | Optional | How to wrap prose (Markdown)Default: preserve |
| options.htmlWhitespaceSensitivity | string Enum: css, strict, ignore | Optional | Whitespace 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 parsed401
Unauthorized - Invalid or missing API key403
Forbidden - API key is inactive or expired, or the request IP is not allowed429
Too Many Requests - Rate limit exceeded500
Internal Server ErrorCode 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 }
}'Rather use it in your browser? Open the Code Beautifier