/carbonara

API for carbon

Primary LanguageJavaScriptMIT LicenseMIT

Carbonara build

API for carbon.

Getting started

Send POST https://carbonara.vercel.app/api/cook to take an image of code snippet.

How it works

  • Puppeteer visit https://carbon.now.sh.
  • Mapping all params in properly URL params.
  • Taking a screenshot of the editor.

POST /api/cook

Body is JSON with next params:

parameter default type description
code (required) string Code snippet
backgroundColor "rgba(171, 184, 195, 1)" string Hex or rgba color
dropShadow true boolean Turn on/off shadow
dropShadowBlurRadius "68px" string shadow blur radius
dropShadowOffsetY "20px" string shadow offset y
exportSize "2x" string resolution of exported image, e.g. 1x, 3x
fontSize "14px" string font size
fontFamily "Hack" string font family, e.g. JetBrains Mono, Fira Code.
firstLineNumber 1 number first line number
language "auto" string programing language for properly highlighting. See name in carbon exported config. For example bash is named "application/x-sh"
lineHeight "133%" string line height
lineNumbers false boolean turn on/off line number
paddingHorizontal "56px" string horizontal padding
paddingVertical "56px" string vertical padding
theme "seti" string code theme
watermark false boolean turn on/off watermark
widthAdjustment true boolean turn on/off width adjustment
windowControls true boolean turn on/off window controls
windowTheme "none" string window theme

Defaults params are the same as https://carbon.now.sh.

Response is an image of a code snippet.

Example

Creating image and saving to code.png in terminal.

curl -L https://carbonara.vercel.app/api/cook \
-X POST \
-H 'Content-Type: application/json' \
-d '{
      "code": "export default const sum = (a, b) => a + b",
      "backgroundColor": "#1F816D"
    }' \
> code.png

Easy way to tune image

  1. Visit https://carbon.now.sh.
  2. Set appearance.
  3. Click gear → miscexport config for downloading JSON with the current setting.
  4. Add code property in JSON.
  5. Use JSON in /api/cook request body.

Unsupported params

These options exist in exported config but there is not a possibility pass them via URL: backgroundImage, backgroundImageSelection, backgroundMode, squaredImage, hiddenCharacters, name, loading, icon, isVisible, width.

Docker container

Running the server in 3000 port with docker:

docker run -p 3000:3000 -it --cap-add=SYS_ADMIN petersolopov/carbonara

Running test

docker build -t local/carbonara .
docker run -it --rm --cap-add=SYS_ADMIN local/carbonara npm test

Development

There are two main files:

  • api/cook.js — lambda function that running in production.
  • src/index.js – nodejs server that running in docker container.

Running development server:

docker build -t local/carbonara .
docker run -v $(pwd):/home/pptruser/app/ -p 3000:3000 -it --rm --cap-add=SYS_ADMIN local/carbonara npm run nodemon

Updating test images:

docker build -t local/carbonara .
docker run -v $(pwd):/home/pptruser/app/ -it --rm --cap-add=SYS_ADMIN local/carbonara npm test

LICENSE

MIT.