Source Code Shots

API to generate beautiful images of your source code.

Documentation

Source Code Shots is free for personal projects with reasonable usage. Please contact me before sending large volumes of requests or if you would like to use Source Code Shots at your company.

Simple Image URL

If you have a short snippet of code, you can create a simple url that will return an image. Only use this method if the resulting URL is less than 2083 characters, otherwise some browsers may reject your request. If it is over 4kb, the server will also reject the request.

This method is ideal for creating dynamic urls to put in the src prop of animg tag.

Path
/api/image
Query Parameters
ParamTypeExample
codestring (required)
console.log(%22hello%20world%22)
languageSupported Languagejs
themeSupported Theme
dark-plus
tabWidthPositive integer
2
Example
https://sourcecodeshots.com/api/image?language=js&theme=dark-plus&code=console.log(%22hello%20world%22)
<img src="https://sourcecodeshots.com/api/image?language=js&theme=dark-plus&code=console.log(%22hello%20world%22)" />

POST Request

If you have a larger code snippet, you can use a POST request to get image data.

Post requests accept an object with code and settings as JSON and return an image.

Path
/api/image
Headers
HeaderValue
Content-Typeapplication/json
Body
TypeExample
{
  code: string;
  settings: {
    language: Supported Language,
    theme: Supported Theme,
    tabWidth: integer (defaults to 2)
  }
}
{
  code: "console.log("hello world"),
  settings: {
    language: "js",
    theme: "dark-plus"
  }
}
Example
async function fetchImage() {
  const response = await fetch('https://sourcecodeshots.com/api/image', {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify({
      code: 'console.log("hello world")',
      settings: {
        language: 'js',
        theme: 'dark-plus',
      },
    }),
  });
  return await response.blob();
}

Get a permanent URL

You can use the same request body as the POST example above to get a permanent URL for your image. You can share the permanent url on social media, put it in an img on a blog, add it to a GitHub issue, etc.

The request accepts an object with code and settings as JSON and returns JSON with a `url` key.

Path
/api/image/permalink
Headers
HeaderValue
Content-Typeapplication/json
Body
TypeExample
{
  code: string;
  settings: {
    language: Supported Language,
    theme: Supported Theme,
    tabWidth: integer (defaults to 2)
  }
}
{
  code: "console.log("hello world"),
  settings: {
    language: "js",
    theme: "dark-plus"
  }
}
Response Body
TypeExample
{
  url: string;
}
{
  url: "https://sourcecodeshots.com/image/yvnYTD8AWdzl2SRIBdwH.png"
}
Example
async function fetchPermanentUrl() {
  const response = await fetch(
    'https://sourcecodeshots.com/api/image/permalink',
    {
      method: 'POST',
      headers: {'Content-Type': 'application/json'},
      body: JSON.stringify({
        code: 'console.log("hello world")',
        settings: {
          language: 'js',
          theme: 'dark-plus',
        },
      }),
    },
  );
  const json = await response.json();
  return json.url;
}

# Highlight code

You can also use the API to highlight code using the token info API. Submit code to the API and it will return a list of tokens with styling information.

Path
/api/token-info
Headers
HeaderValue
Content-Typeapplication/json
Body
TypeExample
{
  code: string;
  settings: {
    language: Supported Language,
    theme: Supported Theme,
    tabWidth: integer (defaults to 2)
  }
}
{
  code: "console.log("hello world"),
  settings: {
    language: "js",
    theme: "dark-plus"
  }
}
Response Body
TypeExample
{
  backgroundColor: string;
  foregroundColor: string;
  windowTheme: 'light' | 'dark';
  tokens: Array<
    Array<{
      text: string;
      foregroundColor: string;
      backgroundColor: string;
      isItalic: boolean;
      isBold: boolean;
      isUnderlined: boolean;
    }>
  >
}
{
  "backgroundColor": "#1E1E1E",
  "foregroundColor": "#D4D4D4",
  "backdropColor": "transparent",
  "windowTheme": "dark"
  "tokens": [
    [
      {
        "text": "console",
        "foregroundColor": "#4EC9B0",
        "backgroundColor": "#1E1E1E",
        "isItalic": false,
        "isBold": false,
        "isUnderlined": false
      },
      {
        "text": ".",
        "foregroundColor": "#D4D4D4",
        "backgroundColor": "#1E1E1E",
        "isItalic": false,
        "isBold": false,
        "isUnderlined": false
      },
      {
        "text": "log",
        "foregroundColor": "#DCDCAA",
        "backgroundColor": "#1E1E1E",
        "isItalic": false,
        "isBold": false,
        "isUnderlined": false
      },
      {
        "text": "(",
        "foregroundColor": "#D4D4D4",
        "backgroundColor": "#1E1E1E",
        "isItalic": false,
        "isBold": false,
        "isUnderlined": false
      },
      {
        "text": ""hello world"",
        "foregroundColor": "#CE9178",
        "backgroundColor": "#1E1E1E",
        "isItalic": false,
        "isBold": false,
        "isUnderlined": false
      },
      {
        "text": ")",
        "foregroundColor": "#D4D4D4",
        "backgroundColor": "#1E1E1E",
        "isItalic": false,
        "isBold": false,
        "isUnderlined": false
      }
    ]
  ]
}
Example
async function highlightCode(code: string) {
  const response = await fetch('https://sourcecodeshots.com/api/token-info', {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify({
      code: code,
      settings: {
        language: 'js',
        theme: 'dark-plus',
      },
    }),
  });
  const tokenInfo = await response.json();

  return (
    <pre
      style={{
        color: tokenInfo.foregroundColor,
        background: tokenInfo.backgroundColor,
      }}>
      <code>
        {tokenInfo.tokens.map((lineTokens, idx) => {
          return (
            <React.Fragment key={idx}>
              {lineTokens.map((token, tokenIdx) => {
                const style = {
                  color: token.foregroundColor,
                };
                if (token.isItalic) {
                  style.fontStyle = 'italic';
                }
                if (token.isBold) {
                  style.fontWeight = 'bold';
                }
                if (token.isUnderlined) {
                  style.textDecoration = 'underline';
                }
                return (
                  <span key={tokenIdx} style={style}>
                    {token.text}
                  </span>
                );
              })}
              {idx === tokenInfo.tokens.length - 1 ? null : '\n'}
            </React.Fragment>
          );
        })}
      </code>
    </pre>
  );
}

Supported Themes

These are all of the themes that the API supports. Use the value in the Id column when sending a request to the API.

Contact me if you would like a theme not listed here.

NameIdExample
Abyssabyss
Aylinaylin
Dark+ (default dark)dark-plus
Light+ (default light)light-plus
GitHub Darkgithub-dark
GitHub Lightgithub-light
Dark (Visual Studio)visual-studio-dark
Light (Visual Studio)visual-studio-light
High Contrasthigh-contrast
Kimbie Darkkimbie-dark
Monokai Dimmeddimmed-monokai
Monokaimonokai
Night Owlnight-owl
Night Owl (No Italics)night-owl-no-italic
Night Owl Lightnight-owl-light
Night Owl Light (No Italics)night-owl-light-no-italic
Quiet Lightquietlight
Redred
Solarized Darksolarized-dark
Solarized Lightsolarized-light
Tomorrow Night Bluetomorrow-night-blue

Supported Languages

These are all of the languages that the API supports. Use the value in the Id column when sending a request to the API.

Contact me if you would like a language not listed here.

NameIdExample
Adaada
Batchbatch
Cc
C#csharp
C++cpp
Clojureclojure
COBOLcobol
CoffeeScriptcoffee
Common Lispcommonlisp
Crystalcrystal
CSScss
Cucumber (Gherkin)gherkin
Ddscript
Dartdart
Diffdiff
Djangodjango
Dockerfiledockerfile
Elixirelixir
Elmelm
Erlangerlang
F#fsharp
Fortranfortran
Git Commitgit-commit
Git Rebasegit-rebase
Gogo
GraphQLgraphql
Groovygroovy
Handlebarshandlebars
Haskellhaskell
HLSLhlsl
HTMLhtml
Ignoreignore
INIcfg
Javajava
JavaScriptjs
JSONjson
JSON5jsonwithcomments
JSXjsx
Juliajulia
Kotlinkts
LaTexlatex
Lessless
Loglog
Lualua
Makefilemakefile
Markdownmarkdown
Mathematicamathematica
N-Triplesntriples
NGINX Confnginx
Nimnim
Objective-Cobjective-c
Objective-C++objective-cpp
OCamlocaml
Octaveoctave
Pascalpascal
Perlperl
PHPphp
Plain Texttext
Powershellpowershell
Pythonpython
Rr
Racketracket
Raku (Perl 6)perl6
Reasonreason
Reason Lispreason_lisp
ReScriptrescript
RISC-Vriscv
Rubyruby
Rustrust
Sasssass
Scalascala
Schemescheme
SCSSscss
ShaderLabshaderlab
Shellshellscript
SmallTalksmalltalk
SQLsql
Swiftswift
TCLtcl
TOMLtoml
TSXtsx
Twigtwig
TypeScriptts
Verilogverilog
VHDLvhdl
Visual Basicvisualbasic
Vue HTMLvue
XMLxml
XQueryxquery
Yamlyaml
Zigzig