# base64\_encode

Encodes a string to Base64 format. Useful for encoding data for APIs, creating basic authentication headers, or encoding binary data for transmission.

```liquid
{% assign encoded = "Hello, World!" | base64_encode %}
{% log encoded %}
```

Output:

```
SGVsbG8sIFdvcmxkIQ==
```

#### Syntax

```liquid
{{ string | base64_encode }}
```

| Parameter | Description        |
| --------- | ------------------ |
| `string`  | The text to encode |

#### Examples

**Create Basic Authentication header:**

```liquid
{% assign credentials = "username:password" | base64_encode %}
{% assign auth_header = "Basic " | append: credentials %}

{% json http_options %}
  {
    "url": "https://api.example.com/data",
    "method": "GET",
    "headers": {
      "Authorization": "{{ auth_header }}"
    }
  }
{% endjson %}

{% http options: http_options as response %}
```

**Encode API credentials:**

```liquid
{% assign api_key = "my_api_key" %}
{% assign api_secret = "my_api_secret" %}
{% assign credentials = api_key | append: ":" | append: api_secret | base64_encode %}

{% log "Encoded credentials: " | append: credentials %}
```

**Encode JSON payload for URL parameter:**

```liquid
{% json payload %}
  {
    "order_id": {{ order.id }},
    "customer_email": {{ customer.email | json }}
  }
{% endjson %}

{% assign encoded_payload = payload | json | base64_encode %}
{% assign callback_url = "https://myapp.com/callback?data=" | append: encoded_payload %}
```

**Encode data for webhook signature:**

```liquid
{% assign timestamp = "now" | date: "%s" %}
{% assign payload_string = timestamp | append: "." | append: request_body %}
{% assign encoded_data = payload_string | base64_encode %}

{% log "Encoded payload for signing: " | append: encoded_data %}
```

**Create encoded return URL:**

```liquid
{% assign return_url = shop.url | append: "/apps/myapp/callback" %}
{% assign encoded_return = return_url | base64_encode %}
{% assign oauth_url = "https://auth.example.com/authorize?return=" | append: encoded_return %}
```

**Encode file content reference:**

```liquid
{% assign file_path = "reports/daily_sales.csv" %}
{% assign encoded_path = file_path | base64_encode %}

{% json request %}
  {
    "file_reference": "{{ encoded_path }}",
    "action": "download"
  }
{% endjson %}
```

**Encode special characters for safe transmission:**

```liquid
{% assign message = "Price: $100 (50% off!) <limited>" %}
{% assign safe_message = message | base64_encode %}

{% log "Original: " | append: message %}
{% log "Encoded: " | append: safe_message %}
```

Output:

```
Original: Price: $100 (50% off!) <limited>
Encoded: UHJpY2U6ICQxMDAgKDUwJSBvZmYhKSA8bGltaXRlZD4=
```

**Encode Unicode/international characters:**

```liquid
{% assign greeting = "こんにちは世界" %}
{% assign encoded_greeting = greeting | base64_encode %}

{{ encoded_greeting }}
```

Output:

```
44GT44KT44Gr44Gh44Gv5LiW55WM
```

#### Common Use Cases

| Use Case           | Example                                |
| ------------------ | -------------------------------------- |
| HTTP Basic Auth    | \`"user:pass"                          |
| API tokens         | \`"api\_key:secret"                    |
| URL-safe data      | Encode JSON for query strings          |
| Webhook signatures | Encode payload before signing          |
| Data obfuscation   | Hide readable values (not encryption!) |

#### Notes

* Supports UTF-8 encoding (handles international characters correctly)
* Output is standard Base64 (uses `+`, `/`, and `=` padding)
* For URL-safe Base64, you may need to replace characters: `| replace: "+", "-" | replace: "/", "_"`
* Base64 encoding increases size by approximately 33%
* This is encoding, not encryption - data can be easily decoded
* See also: `base64_decode` for decoding Base64 strings
