# storage\_write

Writes a file to DataJet storage. You can save content directly or download from a URL. Files can optionally be made publicly accessible.

```liquid
{% capture csv_content %}id,name,price
1,Product A,29.99
2,Product B,49.99{% endcapture %}

{% storage_write filename: "products.csv", content: csv_content as result %}

{% if result.ok %}
  {% log "File saved: " | append: result.file.name %}
{% endif %}
```

#### Syntax

```liquid
{% storage_write filename: "filename", content: content_variable as result_variable %}
{% storage_write filename: "filename", url: "https://example.com/file.csv" as result_variable %}
{% storage_write filename: "filename", content: content_variable, public: true as result_variable %}
```

#### Parameters

| Parameter  | Required | Description                                            |
| ---------- | -------- | ------------------------------------------------------ |
| `filename` | Yes      | Name of the file to store in DataJet storage           |
| `content`  | Yes\*    | String content to save                                 |
| `url`      | Yes\*    | URL to download and save the file from                 |
| `public`   | No       | Set to `true` to make file publicly accessible via URL |

\*Either `content` or `url` is required, but not both.

#### Result Object

| Property      | Type        | Description                                         |
| ------------- | ----------- | --------------------------------------------------- |
| `ok`          | boolean     | `true` if write succeeded                           |
| `error`       | string/null | Error message if `ok` is `false`, `null` on success |
| `file.name`   | string      | The filename                                        |
| `file.public` | boolean     | Whether the file is publicly accessible             |
| `file.url`    | string/null | Public URL (only present if `public: true`)         |

Example result:

```json
{
  "ok": true,
  "error": null,
  "file": {
    "name": "export.csv",
    "public": false,
    "url": null
  }
}
```

Example result with public URL:

```json
{
  "ok": true,
  "error": null,
  "file": {
    "name": "report.pdf",
    "public": true,
    "url": "https://storage.datajet-app.com/..."
  }
}
```

#### Examples

**Save text content:**

```liquid
{% capture report %}
Order Report - {{ 'now' | date: "%Y-%m-%d" }}
================================
Total Orders: {{ orders.size }}
Total Revenue: {{ total_revenue }}
{% endcapture %}

{% storage_write filename: "daily_report.txt", content: report as result %}

{% if result.ok %}
  {% log "Report saved successfully" %}
{% else %}
  {% log "Error: " | append: result.error %}
{% endif %}
```

**Save CSV data:**

```liquid
{% capture csv_data %}sku,quantity,updated_at
{% for item in inventory %}{{ item.sku }},{{ item.quantity }},{{ 'now' | date: "%Y-%m-%dT%H:%M:%SZ" }}
{% endfor %}{% endcapture %}

{% storage_write filename: "inventory_export.csv", content: csv_data as result %}
```

**Save JSON data:**

```liquid
{% json export_data %}
  {
    "exported_at": "{{ 'now' | date: '%Y-%m-%dT%H:%M:%SZ' }}",
    "orders": {{ orders | json }},
    "total_count": {{ orders.size }}
  }
{% endjson %}

{% assign json_content = export_data | json %}
{% storage_write filename: "orders_backup.json", content: json_content as result %}
```

**Save with dynamic filename:**

```liquid
{% assign timestamp = "now" | date: "%Y%m%d_%H%M%S" %}
{% assign filename = "export_" | append: timestamp | append: ".csv" %}

{% storage_write filename: filename, content: csv_content as result %}

{% log "Saved as: " | append: result.file.name %}
```

**Download from URL and save:**

```liquid
{% assign file_url = "https://api.example.com/reports/latest.pdf" %}

{% storage_write filename: "latest_report.pdf", url: file_url as result %}

{% if result.ok %}
  {% log "File downloaded and saved" %}
{% else %}
  {% log "Download failed: " | append: result.error %}
{% endif %}
```

**Save as public file with shareable URL:**

```liquid
{% capture html_content %}
<!DOCTYPE html>
<html>
<head><title>Order Confirmation</title></head>
<body>
  <h1>Order {{ order.name }}</h1>
  <p>Thank you for your order!</p>
</body>
</html>
{% endcapture %}

{% storage_write filename: "confirmation.html", content: html_content, public: true as result %}

{% if result.ok %}
  {% log "Public URL: " | append: result.file.url %}

  {% comment %} Send URL in email or webhook {% endcomment %}
{% endif %}
```

**Save API response to storage:**

```liquid
{% http url: "https://api.example.com/data", method: "GET" as response %}

{% if response.ok %}
  {% assign content = response.body | json %}
  {% storage_write filename: "api_response.json", content: content as result %}
{% endif %}
```

**Save FTP download to storage:**

```liquid
{% ftp_session host: FTP_HOST, user: FTP_USER, password: FTP_PASSWORD %}
  {% ftp_download from: "/reports/data.csv" as download %}

  {% if download.ok %}
    {% assign file_content = download.file.name | content %}
    {% storage_write filename: "ftp_backup.csv", content: file_content as save_result %}

    {% if save_result.ok %}
      {% log "FTP file backed up to storage" %}
    {% endif %}
  {% endif %}
{% endftp_session %}
```

**Create public download link for customer:**

```liquid
{% capture invoice %}
INVOICE #{{ order.name }}
Date: {{ order.created_at | date: "%B %d, %Y" }}
-----------------------------------------
{% for item in order.line_items %}
{{ item.title }} x {{ item.quantity }} - {{ item.price | money }}
{% endfor %}
-----------------------------------------
Total: {{ order.total_price | money }}
{% endcapture %}

{% assign invoice_filename = "invoice_" | append: order.order_number | append: ".txt" %}
{% storage_write filename: invoice_filename, content: invoice, public: true as result %}

{% if result.ok %}
  {% comment %} Include download link in customer email {% endcomment %}
  {% assign download_url = result.file.url %}
{% endif %}
```

**Overwrite existing file:**

```liquid
{% comment %} storage_write overwrites if file exists {% endcomment %}
{% storage_read filename: "counter.txt" as read_result %}

{% if read_result.ok %}
  {% assign count = read_result.file.content | plus: 1 %}
{% else %}
  {% assign count = 1 %}
{% endif %}

{% storage_write filename: "counter.txt", content: count as write_result %}
{% log "Counter updated to: " | append: count %}
```

**Error handling:**

```liquid
{% storage_write filename: "important_data.json", content: json_data as result %}

{% unless result.ok %}
  {% log "CRITICAL: Failed to save file" %}
  {% log result.error %}

  {% comment %} Trigger error handler {% endcomment %}
  {% json error_payload %}
    {
      "operation": "storage_write",
      "filename": "important_data.json",
      "error": "{{ result.error }}",
      "timestamp": "{{ 'now' | date: '%Y-%m-%dT%H:%M:%SZ' }}"
    }
  {% endjson %}

  {% run "error_handler", error_payload %}
{% endunless %}
```

#### Related Tags

| Tag            | Description                        |
| -------------- | ---------------------------------- |
| `storage_read` | Reads content from DataJet storage |

#### Notes

* Consumes 3 credits per write operation
* Writing to an existing filename will overwrite the file
* Use `public: true` to generate a shareable URL
* Public URLs are permanent until the file is deleted or overwritten
* Use `content` for text/string data, use `url` to download and save remote files
* Maximum file size limits apply based on your plan
