ContentImageByKey
Returns an HTML img tag for a Content Builder image asset by its customer key. Runtime-proven on a live Marketing Cloud Engagement CloudPage — including the fallback-image path and that a missing image with no fallback aborts the page.
Syntax
ContentImageByKey(imageExternalKey[, defaultImageExternalKey]) → string
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
imageExternalKey |
string | Yes | Customer key of the Content Builder image asset to return |
defaultImageExternalKey |
string | No | Customer key of a fallback image asset, rendered when the primary key is not found |
Example
%%=ContentImageByKey('sfdcmarketingcloud-518005426')=%%
Renders an HTML img tag for that asset — in this run the tag carried title, alt, border="0" and the proprietary thid attribute, with the asset’s stored src URL.
A defensive form supplies a fallback image that renders when the primary key is missing:
%%=ContentImageByKey('nosuch-key', 'copado-logo.png-518005426')=%%
The primary key does not exist, so the tag for the fallback asset (thid="1201143") is rendered instead.
Return value
string — an HTML img tag for the referenced image, with title, alt, border and thid attributes set. There is no closed set of sentinel values.
Behaviour
A single customer key renders the image as an img tag. The output is full HTML markup, not a bare URL — the title and alt attributes carry the asset name, border is always 0, and thid holds the Content Builder image ID. The key form is the most portable because it travels with the asset across business units.
The second argument is a fallback image, not alt text. When the primary key is missing but a valid fallback key is supplied, the fallback image’s tag is rendered.
A missing image with no fallback aborts the page. Unlike the ContentBlock family, there is no default-content or empty-string mechanism — a missing key with a single argument aborts rendering (HTTP 422 on a CloudPage). Always supply a fallback when the key may not resolve.
Show test script
%%[
VAR @b, @found, @fallback
SET @b = RequestParameter("b")
/* safe sweep: the found render and the fallback path are both accepted */
IF @b == "safe" THEN
/* found: a single key renders the stored image as an img tag */
SET @found = ContentImageByKey("sfdcmarketingcloud-518005426")
OutputLine(Concat("found=[", @found, "]"))
/* fallback: missing primary key + valid fallback key renders the fallback img tag */
SET @fallback = ContentImageByKey("nosuch-key", "copado-logo.png-518005426")
OutputLine(Concat("fallback=[", @fallback, "]"))
ENDIF
]%%
Availability
| Platform | Available |
|---|---|
| Marketing Cloud Engagement | Yes |
| Marketing Cloud Next | No |
See also
ContentImageByID— return the same image by its numeric ID- Official reference · ampscript.guide