Skip to content
moxon home

Badge

The /badge endpoint draws a standalone SVG badge, a pill with a label, optional icons and an optional second part, for a plain <img>.

gethttps://api.moxon.dev/v6/badge
Try itget/v6/badge

Parameters

Only label is required. Every other parameter changes one aspect of the badge, and a badge without any of them is a dark pill with white bold text.

Text and icons

  • labelstringrequired
    —

    Text of the badge, up to 64 characters

  • iconstringoptional
    —

    Name of an icon drawn before the label

  • trailstringoptional
    —

    Name of an icon drawn after the text, after the message when there is one

  • messagestringoptional
    —

    Text of a second part, drawn after the label on its own background, up to 64 characters

A label or a message cannot hold control characters or invisible ones, and needs at least one character that draws something (see the box below). Runs of spaces count as one, and an accented letter sent as a letter followed by its accent is drawn like the single accented letter.

Characters that are refused

A label or a message with one of these characters is refused with a 400:

  • Control characters, line and paragraph separators, and lone surrogates.
  • Format characters that draw nothing: the soft hyphen, the zero-width space, the word joiner, the bidirectional marks and the tag characters.
  • Invisible fillers: the Hangul fillers, the blank Braille pattern and the combining grapheme joiner.
  • The noncharacters U+FFFE and U+FFFF.

A text made only of spaces is refused as a missing text, and one made only of combining marks or variation selectors is refused like the characters above, as it draws nothing.

Still accepted: an emoji or a symbol followed by its variation selector (U+FE0E and U+FE0F, as in the red heart), accented letters, and no-break or ideographic spaces around a visible text.

Colors

  • startstringoptional

    Default: #2d333b.

    Background color, or the first color of its gradient

  • endstringoptional

    Default: start.

    Last color of the gradient. Without it, the background is a single color

  • inkstringoptional

    Default: #ffffff.

    Color of the text and of the icons, except the trailing icon when there is a message

  • messageStartstringoptional

    Default: #0969da.

    Background color of the second part, or the first color of its gradient

  • messageEndstringoptional

    Default: messageStart.

    Last color of the gradient of the second part

  • messageInkstringoptional

    Default: ink.

    Color of the text of the second part and, with a message, of the trailing icon. Ignored without a message

  • borderstringoptional
    —

    Color of a 1 px outline. Without it, the badge has no outline

A color is a hexadecimal value of 3 or 6 digits, with or without the #. In a URL, write the # as %23, or leave it out. The colors of the second part are checked even without a message.

Shape and font

  • sizestringoptional

    Default: sm.

    xs, sm, md or lg: a badge 16, 20, 24 or 28 px high. The icons and the text follow

    • xs
    • sm
    • md
    • lg
  • radiusnumberoptional

    Default: pill.

    Corner radius in pixels, from 0 (square) to 100. A radius larger than half the height gives the pill, which is also the default

  • anglenumberoptional

    Default: 135.

    Direction of the gradients in degrees, from 0 to 360, in the CSS convention: 90 runs from left to right and 180 from top to bottom. The default runs from corner to corner. The value is rounded to the nearest whole degree

  • liftnumberoptional

    Default: 0.

    From 0 to 10: the image grows by twice this value in height, which leaves that much free space under the badge, and the badge stays at the top

  • font"sans" | "serif" | "mono"optional

    Default: sans.

    sans, serif or mono

  • weight"normal" | "bold"optional

    Default: bold.

    normal or bold

Good to know

radius, angle and lift take a plain decimal of up to three digits and two decimals (4, 12.5): a sign, an exponent or an empty value is refused. An empty size, font or weight is refused too, whereas an empty color, icon, border or message is ignored.

The width of the badge is measured on the server, so it is the same wherever it is shown. The text is drawn in the first font of its family that the viewer has installed (Verdana for sans, Georgia for serif, Menlo for mono), and it is stretched to the measured width, so a different font only changes the spacing between the letters.

Icons

icon and trail take the name of one of the 106 icons bundled with the API: 105 icons of Lucide and discord. Any other name is refused. The icons are drawn in the ink color, in the size of the badge, and the trailing one in messageInk when there is a message.

Click to expand all available icons

  • Arrows and actions:
    • arrow-down
    • arrow-left
    • arrow-right
    • arrow-up
    • check
    • chevron-right
    • download
    • external-link
    • minus
    • plus
    • refresh-cw
    • upload
    • x
  • Status and distinctions:
    • award
    • badge-check
    • bell
    • bug
    • circle-alert
    • circle-check
    • circle-dot
    • circle-x
    • construction
    • flag
    • flame
    • heart
    • info
    • shield
    • shield-check
    • sparkles
    • star
    • thumbs-up
    • triangle-alert
    • trophy
    • zap
  • Everyday objects and media:
    • calendar
    • clock
    • cloud
    • coffee
    • eye
    • gift
    • globe
    • hourglass
    • house
    • image
    • mail
    • message-circle
    • monitor
    • moon
    • music
    • palette
    • pause
    • pencil
    • play
    • rocket
    • smartphone
    • sun
    • timer
    • trash-2
    • user
    • users
    • video
    • wifi
  • Brands:
    • chrome
    • codepen
    • discord
    • dribbble
    • facebook
    • figma
    • framer
    • github
    • gitlab
    • instagram
    • linkedin
    • slack
    • trello
    • twitch
    • twitter
    • youtube

The icons of Lucide are published under the ISC licence, and the ones it derives from Feather under the MIT licence: both licences are kept in the source code of the API.

Two parts

With a message, the badge is split in two like the ones of shields.io: the label on the first background, the message on a second one, which has its own colors (messageStart, messageEnd and messageInk). The border and the radius go around the whole badge, and trail is drawn at the very end.

<img src="https://api.moxon.dev/v6/badge?label=build&message=passing&messageStart=16a34a&icon=circle-check" alt="build: passing" />

Response

The response is a standalone SVG (Content-Type: image/svg+xml), sized to its content, with no script and no external file. It is usable in an <img> tag, as a CSS background or in a Markdown image:

<img src="https://api.moxon.dev/v6/badge?label=New&icon=sparkles&start=7c3aed&end=db2777" alt="New" />
![New](https://api.moxon.dev/v6/badge?label=New&icon=sparkles&start=7c3aed&end=db2777)

The image only depends on the query string, so it is sent with Cache-Control: public, max-age=86400 (one day) and an ETag, and with Content-Security-Policy: default-src 'none'.

Code Examples

In Node.js

The badge function takes the same options as the query string, with lift, radius and angle as numbers, and returns { contentType, body }, body being the SVG. It throws an Error with a message of the table below when an option is invalid. Outside HTTP, a number is only checked for its range and rounded to two decimals. It is exported by @moxon/api/v6 and by the package root, which resolves to the latest version, see the npm package.

import { badge } from '@moxon/api/v6';

const { contentType, body } = badge({
    label: 'New',
    icon: 'sparkles',
    start: '#7c3aed',
    end: '#db2777',
    lift: 2,
});

console.log(contentType); // 'image/svg+xml'
console.log(body); // '<svg xmlns="http://www.w3.org/2000/svg" ...'

Error Handling

Every invalid value is refused with a 400 and a message that names it, instead of being replaced by a default.

  • 400Please provide a label (?label={text})The label is missing or empty, holds only spaces, or is given more than once
  • 400Label cannot exceed 64 charactersThe label is longer than 64 characters. The same message exists for the message, as Message cannot exceed 64 characters
  • 400Label cannot contain control or invisible charactersThe label holds a control or invisible character, or nothing but combining marks or variation selectors (see the characters that are refused). The same message exists for the message, as Message cannot contain control or invisible characters
  • 400Please provide a valid messageThe message holds only spaces or is given more than once. An empty message is ignored
  • 400Unknown icon "nope"The icon is not one of the icons. The name is repeated in the message, cut at 40 characters
  • 400Unknown trail icon "nope"The trail is not one of the icons
  • 400Please provide a valid icon nameThe icon is given more than once. The same message exists for the trail, as Please provide a valid trail icon name
  • 400Invalid start color (use hex like ff6600)The start is not a hexadecimal color. The same message exists for end, ink, messageStart, messageEnd, messageInk and border, with the name of the parameter
  • 400Please provide a valid start colorA color is given more than once. The same message exists for the other colors, with the name of the parameter
  • 400Invalid size (use xs, sm, md or lg)The size is not one of the four sizes
  • 400Invalid font (use sans, serif or mono)The font is not one of the three fonts
  • 400Invalid weight (use normal or bold)The weight is neither normal nor bold
  • 400Invalid lift (use a number from 0 to 10)The lift is out of range, not a plain decimal or has more than two decimals
  • 400Invalid radius (use a number from 0 to 100)The radius is out of range, not a plain decimal or has more than two decimals
  • 400Invalid angle (use a number from 0 to 360)The angle is out of range, not a plain decimal or has more than two decimals