Skip to content
generate svg captcha in node
Branch: 1.x
Clone or download
Latest commit f751635 Nov 27, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information.
.vscode [UPDATED] jest vs code debug file Aug 9, 2017
fonts [ADDED] font LICENSE Aug 10, 2016
lib [FIXED] viewbox Nov 27, 2018
test [FIXED] tests on node v4 Aug 9, 2017
.editorconfig [UPDATED] refactor so that user can load a new font Jan 4, 2017
.gitignore [UPDATED] jest vs code debug file Aug 9, 2017
.travis.yml use matrix transformation to replace individual transform May 31, 2016 [UPDATED] readme Sep 9, 2017
yarn.lock [FIXED] viewbox Nov 27, 2018


Build Status NPM Version NPM Downloads

generate svg captcha in node.js



useful if you

  • cannot or do not want to use google recaptcha
  • have issue with install c++ addon


npm install --save svg-captcha


var svgCaptcha = require('svg-captcha');

var captcha = svgCaptcha.create();
// {data: '<svg.../svg>', text: 'abcd'}

with express

var svgCaptcha = require('svg-captcha');

app.get('/captcha', function (req, res) {
	var captcha = svgCaptcha.create();
	req.session.captcha = captcha.text;



If no option is passed, you will get a random string of four characters and corresponding svg.

  • size: 4 // size of random string
  • ignoreChars: '0o1i' // filter out some characters like 0o1i
  • noise: 1 // number of noise lines
  • color: true // characters will have distinct colors instead of grey, true if background option is set
  • background: '#cc9966' // background color of the svg image

This function returns an object that has the following property:

  • data: string // svg path data
  • text: string // captcha text


Similar to create api, you have the same options and return value. The difference is that data is a svg will be an math equation on screen and text will be the result of that equation in string, otherwise the usage is the same as above.


Load your own font and override the default font.

  • url: string // path to your font This api is a wrapper around loadFont api of opentype.js.
    Your may need experiment around various options to make your own font accessible.
    See the following api.


Gain access to global setting object. It is used for create and createMathExpr api as the default options.

In addition to size, noise, color, and background, you can also set the following property:

  • width: number // width of captcha
  • height: number // height of captcha
  • fontSize: number // captcha text size
  • charPreset: string // random character preset


return a random string.

svgCaptcha(text, options)

return a svg captcha based on text provided.

In pre 1.1.0 version you have to call these two functions,
now you can call create() to save some key strokes ;).

sample image

default captcha image:


math expression image with color options:


why use svg?

It does not require any c++ addon.
The result image is smaller than jpeg image.

This has to be a joke. /<text.+>;.+</text>/g.test...

svg captcha uses opentype.js underneath, which means that there is no '<text>1234</text>'.
You get '<path fill="#444" d="M104.83 19.74L107.85 19.74L112 33.56L116.13 19.74L119.15 19.74L113.48 36.85...'

Even though you can write a program that convert svg to png, svg captcha has done its job
—— make captcha recognition harder



You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.