Rather than using append and prepend Jekyll filters, you code can look more natural using capture.

Basic

{%- capture my_var -%}
/api/{{ name }}.json
{%- endcapture -%}

You can do multiple lines.

{%- capture foo -%}
abc {{ name }} def
xyz 123
{%- endcapture -%}

Trim whitespace

That is fine for most purposes.

Note that if you subsitute in a string, you’ll get a multi-line value.

Like

<script>
const value = "
/api/foo.json
"
</script>

So trim it.

{%- capture my_data -%}
/api/{{ name }}
{%- endcapture -%}

Note use of - before and after the value, to avoid the newlines. Using - on the outer of the entire capture doesn’t work.

Or

{% capture my_data %}/api/{{ name }}{% endcapture %}