<p>Assuming this is for Craft 3, you can do something like this from a template to loop through all available cookies for a site:</p>

<pre><code>{% set cookies = craft.app.request.getCookies() %}

{% for cookie in cookies %}
    {{ cookie.name }}
{% endfor %}
</code></pre>

CC BY-SA 4.0
2019-06-25T15:59:08.317Z
That seems to work, but I get back 'CRAFT_CSRF_TOKEN'. I am expecting another value. {% set cookies = craft.app.request.getCookies('invited') %} I'm requesting the cookie like that.
JSosa
CC BY-SA 4.0
2024-01-17T23:12:21.830Z
@JSosa getCookies() returns the collection of cookies. The cookie you want would be cookies['invited'] out of the returned set. Reference: https://www.yiiframework.com/doc/api/2.0/yii-web-request#getCookies()-detail
cherrykoda
CC BY-SA 4.0
2019-06-24T22:23:48.700Z
2019-06-24T22:23:48.700Z
Brad Bell
<p>If you want a specific cookie instead of the set of cookies, you'd use</p>
<p>Old Craft 3 Syntax:</p>
<pre><code>{% set specificCookie = craft.app.request.getCookie('specificCookie') %}
</code></pre>
<p>Deprecation Replacement (for Craft 4 compatibility):</p>
<pre><code>craft.app.request.cookies.get('specificCookie')
</code></pre>
<p>Craft 3 Docs: <a href="https://docs.craftcms.com/api/v3/craft-web-twig-variables-request.html#method-getcookie" rel="nofollow noreferrer">https://docs.craftcms.com/api/v3/craft-web-twig-variables-request.html#method-getcookie</a></p>
<p>Codebase Reference: <a href="https://github.com/craftcms/cms/blob/v3/src/web/twig/variables/Request.php#L212-L217" rel="nofollow noreferrer">https://github.com/craftcms/cms/blob/v3/src/web/twig/variables/Request.php#L212-L217</a></p>

CC BY-SA 4.0
2024-01-17T23:14:14.447Z
2024-01-17T23:14:14.447Z
cherrykoda
<p>This question was already asked a few years ago, and the way of doing it was through a plugin. I was wondering if there is a way of doing it just using Craft and Twig.</p>

<p>I set up a cookie using JavaScript, and I want to read it with Craft. Is this possible? </p>

<craft3><javascript><cookies>
How can I get cookies that were set in javascript from a twig template? [NEW]
# How can I get cookies that were set in javascript from a twig template? [NEW]

(Asked by: username_0 on 2019-06-24)

This question was already asked a few years ago, and the way of doing it was through a plugin. I was wondering if there is a way of doing it just using Craft and Twig.

I set up a cookie using JavaScript, and I want to read it with Craft. Is this possible?

**Tags:** craft3, javascript, cookies

---

## 2 answers

### Answer by username_1 on 2019-06-24. Score: 3

Assuming this is for Craft 3, you can do something like this from a template to loop through all available cookies for a site:
{% set cookies = craft.app.request.getCookies() %}

{% for cookie in cookies %}
    {{ cookie.name }}
{% endfor %}

> **Comments:**
> **username_2**: That seems to work, but I get back 'CRAFT_CSRF_TOKEN'. I am expecting another value. {% set cookies = craft.app.request.getCookies('invited') %} I'm requesting the cookie like that.  
> **username_3**: @JSosa getCookies() returns the collection of cookies. The cookie you want would be cookies['invited'] out of the returned set. Reference: https://www.yiiframework.com/doc/api/2.0/yii-web-request#getCookies()-detail  

---

### Answer by username_3 on 2024-01-17. Score: 2

If you want a specific cookie instead of the set of cookies, you'd use
Old Craft 3 Syntax:
{% set specificCookie = craft.app.request.getCookie('specificCookie') %}

Deprecation Replacement (for Craft 4 compatibility):
craft.app.request.cookies.get('specificCookie')

Craft 3 Docs: https://docs.craftcms.com/api/v3/craft-web-twig-variables-request.html#method-getcookie
Codebase Reference: https://github.com/craftcms/cms/blob/v3/src/web/twig/variables/Request.php#L212-L217


---

