Runtime verified Test scripts included

Syntax

Base64Encode(value[, encoding])  →  string
1–2 arguments

Parameters

Name Type Required Description
value string Yes The value to encode; the empty string encodes to the empty string
encoding string No Name of the character encoding applied before encoding; defaults to UTF-8

Example

%%[
  VAR @encoded
  SET @encoded = Base64Encode("SFMC AMPscript 2026")
]%%
%%=v(@encoded)=%%

Renders U0ZNQyBBTVBzY3JpcHQgMjAyNg==.

The usual reason to reach for it is packing a value into somewhere that only tolerates a limited character set, such as a link:

%%[
  VAR @payload, @token
  SET @payload = Concat(AttributeValue("EmailAddress"), "|", Now())
  SET @token = Base64Encode(@payload)
]%%
<a href="https://example.org/claim?t=%%=v(@token)=%%">Claim your offer</a>

Base64 is an encoding, not a secret: anyone can reverse it. Sign or encrypt anything that must not be tampered with.

Return value

string — the Base64 form of the input, padded with = to a multiple of four characters.

There is no closed set of sentinel values to test for: every accepted input produces an encoded string, and every rejected one aborts the page instead of returning an error token.

Behaviour

The output is standard Base64, verified against an independent implementation. Base64Encode("SFMC AMPscript 2026") gave U0ZNQyBBTVBzY3JpcHQgMjAyNg==, 28 characters measured on the page, character for character the same value the same string produces outside Marketing Cloud.

All three padding shapes come out right. A three-byte input needs no padding (ManTWFu), a two-byte input takes one = (MaTWE=), and a one-byte input takes two (MTQ==).

The bytes encoded are the UTF-8 form of the input. A string containing é and gave Y2Fmw6nigqw=, the encoding of its UTF-8 bytes, not of the UTF-16 form the engine holds internally. This is the detail that decides whether a value survives a round trip through a partner system.

The empty string is encoded, not refused. It returns the empty string, at HTTP 200.

The encoding argument accepts more than is documented

Six names were accepted and each changed the output to the Base64 of exactly those bytes: UTF-8, UTF-16 (little-endian), UTF-16BE, UTF-32, ASCII and ISO-8859-1. The same non-ASCII input gave YwBhAGYA6QCsIA== under UTF-16 and AGMAYQBmAOkgrA== under UTF-16BE — a domain identical to the one the hash family accepts.

ASCII and ISO-8859-1 are lossy and silent about it: they replace every character they cannot represent with a question mark before encoding, giving Y2FmPz8= and Y2Fm6T8= respectively. Nothing warns you.

An unrecognised name is rejected outright — banana aborted the page with HTTP 422 rather than falling back to the default, and so did an empty encoding name.

Show test script
%%[
  VAR @b, @a, @nb
  SET @b = RequestParameter("b")
  SET @a = "SFMC AMPscript 2026"
  SET @nb = Concat("caf", Char(233), Char(8364))

  /* known-good control: renders on every request, so a run of HTTP 422s
     can be told apart from a broken deploy */
  OutputLine(Concat("CTRL=[", Base64Encode("Man"), "]"))

  /* the plain one-argument form, its length, the three padding shapes and
     the UTF-8 default proven with a non-ASCII input */
  IF @b == "safe" THEN
    OutputLine(Concat("--- safe start ---"))
    OutputLine(Concat("BE=[", Base64Encode(@a), "]"))
    OutputLine(Concat("BEL=[", Length(Base64Encode(@a)), "]"))
    OutputLine(Concat("NBIN=[", @nb, "]"))
    OutputLine(Concat("BEN=[", Base64Encode(@nb), "]"))
    OutputLine(Concat("PAD3=[", Base64Encode("Man"), "]"))
    OutputLine(Concat("PAD2=[", Base64Encode("Ma"), "]"))
    OutputLine(Concat("PAD1=[", Base64Encode("M"), "]"))
    OutputLine(Concat("--- safe done ---"))
  ENDIF

  /* the empty string is encoded, not refused */
  IF @b == "empty" THEN
    OutputLine(Concat("--- empty start ---"))
    OutputLine(Concat("BEE=[", Base64Encode(""), "]"))
    OutputLine(Concat("--- empty done ---"))
  ENDIF

  /* the encoding argument is honoured, and the accepted name set is wider
     than either source documents; ASCII and ISO-8859-1 substitute silently */
  IF @b == "enc" THEN
    OutputLine(Concat("--- enc start ---"))
    OutputLine(Concat("BE8=[", Base64Encode(@nb, "UTF-8"), "]"))
    OutputLine(Concat("BE16=[", Base64Encode(@nb, "UTF-16"), "]"))
    OutputLine(Concat("BE16BE=[", Base64Encode(@nb, "UTF-16BE"), "]"))
    OutputLine(Concat("BE32=[", Base64Encode(@nb, "UTF-32"), "]"))
    OutputLine(Concat("BEASC=[", Base64Encode(@nb, "ASCII"), "]"))
    OutputLine(Concat("BEISO=[", Base64Encode(@nb, "ISO-8859-1"), "]"))
    OutputLine(Concat("--- enc done ---"))
  ENDIF

  /* each of the four branches below aborts the page - fetch alone */
  IF @b == "badenc" THEN
    OutputLine(Concat("--- badenc start ---"))
    OutputLine(Concat("BEBAD=[", Base64Encode(@a, "banana"), "]"))
  ENDIF

  IF @b == "emptyenc" THEN
    OutputLine(Concat("--- emptyenc start ---"))
    OutputLine(Concat("BEEMPT=[", Base64Encode(@a, ""), "]"))
  ENDIF

  IF @b == "a0" THEN
    OutputLine(Concat("--- a0 start ---"))
    OutputLine(Concat("A0=[", Base64Encode(), "]"))
  ENDIF

  IF @b == "a3" THEN
    OutputLine(Concat("--- a3 start ---"))
    OutputLine(Concat("A3=[", Base64Encode(@a, "UTF-8", "extra"), "]"))
  ENDIF
]%%

Availability

Platform Available
Marketing Cloud Engagement Yes
Marketing Cloud Next No

See also