> For the complete documentation index, see [llms.txt](https://docs.datajet-app.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.datajet-app.com/liquid/filters/base64_decode.md).

# base64\_decode

### base64\_decode

Decodes a Base64-encoded string back to its original text. Useful for reading encoded API responses, decoding authentication tokens, or processing encoded webhook payloads.

```liquid
{% assign decoded = "SGVsbG8sIFdvcmxkIQ==" | base64_decode %}
{% log decoded %}
```

Output:

```
Hello, World!
```

#### Syntax

```liquid
{{ base64_string | base64_decode }}
```

| Parameter       | Description                         |
| --------------- | ----------------------------------- |
| `base64_string` | The Base64-encoded string to decode |

#### Examples

**Decode API response data:**

```liquid
{% http options: api_request as response %}

{% if response.body.encoded_data %}
  {% assign decoded_data = response.body.encoded_data | base64_decode %}
  {% assign parsed_data = decoded_data | parse %}

  {% log "Decoded response:" %}
  {% log parsed_data %}
{% endif %}
```

**Decode webhook payload:**

```liquid
{% assign encoded_payload = webhook.body.data %}
{% assign decoded_payload = encoded_payload | base64_decode %}
{% assign payload = decoded_payload | parse %}

{% log "Order ID from webhook: " | append: payload.order_id %}
```

**Decode URL parameter data:**

```liquid
{% assign encoded_data = request.params.data %}
{% assign decoded_json = encoded_data | base64_decode %}
{% assign callback_data = decoded_json | parse %}

{% log "Callback received for order: " | append: callback_data.order_id %}
```

**Decode Basic Auth header (for debugging):**

```liquid
{% comment %} Extract Base64 part from "Basic <credentials>" {% endcomment %}
{% assign auth_header = request.headers.authorization %}
{% assign encoded_credentials = auth_header | remove: "Basic " %}
{% assign decoded_credentials = encoded_credentials | base64_decode %}

{% assign parts = decoded_credentials | split: ":" %}
{% assign username = parts[0] %}
{% assign password = parts[1] %}

{% log "Username: " | append: username %}
```

**Process encoded email content:**

```liquid
{% assign encoded_body = email_data.body_base64 %}
{% assign email_body = encoded_body | base64_decode %}

{% log "Email content:" %}
{% log email_body %}
```

**Decode international characters:**

```liquid
{% assign encoded = "44GT44KT44Gr44Gh44Gv5LiW55WM" %}
{% assign decoded = encoded | base64_decode %}
{% log decoded %}
```

Output:

```
こんにちは世界
```

**Decode and parse JSON from encoded parameter:**

```liquid
{% assign encoded_config = metafield.value %}
{% assign config_json = encoded_config | base64_decode %}
{% assign config = config_json | parse %}

{% log "Feature flags:" %}
{% for flag in config.features %}
  {% log flag.name | append: ": " | append: flag.enabled %}
{% endfor %}
```

**Handle URL-safe Base64:**

```liquid
{% comment %} Convert URL-safe Base64 to standard Base64 first {% endcomment %}
{% assign url_safe_encoded = request.params.token %}
{% assign standard_encoded = url_safe_encoded | replace: "-", "+" | replace: "_", "/" %}
{% assign decoded_token = standard_encoded | base64_decode %}

{% log "Decoded token: " | append: decoded_token %}
```

**Decode and validate signature data:**

```liquid
{% assign signature_data = webhook.headers.x-signature-data %}
{% assign decoded_sig_data = signature_data | base64_decode %}
{% assign sig_parts = decoded_sig_data | split: "." %}

{% assign timestamp = sig_parts[0] %}
{% assign payload_hash = sig_parts[1] %}

{% log "Signature timestamp: " | append: timestamp %}
{% log "Payload hash: " | append: payload_hash %}
```

**Decode embedded file content:**

```liquid
{% if attachment.content_base64 %}
  {% assign file_content = attachment.content_base64 | base64_decode %}

  {% comment %} If it's CSV data, parse it {% endcomment %}
  {% assign csv_data = file_content | parse_csv %}

  {% log "Parsed " | append: csv_data.size | append: " rows from attachment" %}
{% endif %}
```

#### Encoding Round-Trip

```liquid
{% assign original = "Hello, World! 你好世界" %}
{% assign encoded = original | base64_encode %}
{% assign decoded = encoded | base64_decode %}

{% if original == decoded %}
  {% log "Round-trip successful!" %}
{% endif %}

{% log "Original: " | append: original %}
{% log "Encoded:  " | append: encoded %}
{% log "Decoded:  " | append: decoded %}
```

Output:

```
Round-trip successful!
Original: Hello, World! 你好世界
Encoded:  SGVsbG8sIFdvcmxkISDkvaDlpb3kuJbnlYw=
Decoded:  Hello, World! 你好世界
```

#### Error Handling

```liquid
{% assign potentially_encoded = some_variable %}

{% comment %} Base64 strings typically end with = padding or have specific length {% endcomment %}
{% assign is_likely_base64 = false %}
{% if potentially_encoded contains "==" or potentially_encoded.size | modulo: 4 == 0 %}
  {% assign is_likely_base64 = true %}
{% endif %}

{% if is_likely_base64 %}
  {% assign decoded = potentially_encoded | base64_decode %}
  {% log decoded %}
{% else %}
  {% log "Value does not appear to be Base64 encoded" %}
{% endif %}
```

#### Notes

* Supports UTF-8 decoding (handles international characters correctly)
* Expects standard Base64 input (with `+`, `/`, and `=` padding)
* For URL-safe Base64, convert `-` to `+` and `_` to `/` before decoding
* Invalid Base64 input may produce unexpected results or empty output
* Decoding does not validate the content - always validate decoded data before use
* See also: `base64_encode` for encoding strings to Base64
