# settings variable in js file

**URL:** <https://forum.djangoproject.com/t/settings-variable-in-js-file/12262>\
**Category:** Templates & Frontend\
**Created:** [February 20, 2022, 4:06pm UTC](https://forum.djangoproject.com/t/settings-variable-in-js-file/12262 "2022-02-20T16:06:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![567](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/567/32/7548_2.png) [@567](https://forum.djangoproject.com/u/567)\
**Post date:** [February 20, 2022, 4:06pm UTC](https://forum.djangoproject.com/t/settings-variable-in-js-file/12262/1 "2022-02-20T16:06:39Z")

</div>

I have an API key declared in settings.py as a variable.  
I need that API key in a call to an API which takes place in a JS file.  
How can I access that settings variable in the JS file?  
What is a secure way to do this?  
Thank you

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 20, 2022, 5:33pm UTC](https://forum.djangoproject.com/t/settings-variable-in-js-file/12262/2 "2022-02-20T17:33:46Z")

</div>

The generally-accepted “safe” method for injecting Django data into a template for access by a JavaScript function is by using the [json\_script filter](https://docs.djangoproject.com/en/4.0/ref/templates/builtins/#json-script).

But I’m not sure what you mean by secure. If you’re looking to prevent someone from looking at the source for your page and finding your API key, then you need to take a different approach.

---

<div class="post-metadata">

**Author:** ![567](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/567/32/7548_2.png) [@567](https://forum.djangoproject.com/u/567)\
**Post date:** [February 20, 2022, 7:12pm UTC](https://forum.djangoproject.com/t/settings-variable-in-js-file/12262/3 "2022-02-20T19:12:19Z")

</div>

Thank you, I would use the json\_script filter.  
The issue that I have now is how to import the variable from settings.py into the template.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 20, 2022, 7:13pm UTC](https://forum.djangoproject.com/t/settings-variable-in-js-file/12262/4 "2022-02-20T19:13:35Z")

</div>

Like all other data being rendered in the template, it gets passed to the rendering engine through the context.  
Import the settings in the view and add that setting to the context. It is then available to be rendered by the template.
