# Best way to handle user constants (e.g. API key)?

**URL:** https://discourse.julialang.org/t/best-way-to-handle-user-constants-e-g-api-key/20912
**Category:** New to Julia
**Tags:** constants
**Created:** [February 17, 2019, 8:53pm UTC](https://discourse.julialang.org/t/best-way-to-handle-user-constants-e-g-api-key/20912 "2019-02-17T20:53:35Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![dopamine](https://avatars.discourse-cdn.com/v4/letter/d/e9a140/32.png) [@dopamine](https://discourse.julialang.org/u/dopamine)
#### Post date: [February 17, 2019, 8:53pm UTC](https://discourse.julialang.org/t/best-way-to-handle-user-constants-e-g-api-key/20912/1 "2019-02-17T20:53:35Z")

</div>

When developing a package, what is the convention/best way to handle user constants? e.g. API key, user name, etc.

---

<div class="post-metadata">

### Author: ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)
#### Post date: [February 17, 2019, 9:29pm UTC](https://discourse.julialang.org/t/best-way-to-handle-user-constants-e-g-api-key/20912/2 "2019-02-17T21:29:44Z")

</div>

An easy way is to just have constants `const API_KEY = ...` etc in your code. But if this is sensitive or user specific information that you don’t want checked in, you’ll have to remove/re-add these fields every time you check in, which is a hassle. A better solution then is to:

- Put these fields in a config file, using a readable format with support for comments (not JSON!). For example YAML or XML.
- Assuming that the fields are sensitive (API key), don’t check the file in. Add it to `.gitignore`.
- If the config file is missing, as will be the case when people first install your package, make your script give very detailed error messages for what it expects. You can for example have a `template.yml` file that the user can copy and populate. Or make your script copy it, or ask for the information and populate the config file.
- If you ever checked in sensitive information, and then removed it, [think again](https://github.com/search?q=remove+password&type=Commits)

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [February 19, 2019, 9:44am UTC](https://discourse.julialang.org/t/best-way-to-handle-user-constants-e-g-api-key/20912/3 "2019-02-19T09:44:03Z")

</div>

An environment variable, which the user can set in `startup.jl` or outside Julia.

---

<div class="post-metadata">

### Author: ![swissr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swissr/32/208_2.png) [@swissr](https://discourse.julialang.org/u/swissr)
#### Post date: [February 19, 2019, 10:45am UTC](https://discourse.julialang.org/t/best-way-to-handle-user-constants-e-g-api-key/20912/4 "2019-02-19T10:45:41Z")

</div>

In a (non Julia package related repo) I used `git-crypt` [1] for sensitive config data. A bit of a quick and dirty distribution of login credentials among people who have (locally) the key.

Lacking the key the file content is garbled and can be removed again ‘without thinking’ from the repository. It’s certainly not a convention or a common practice for package user constants, but might come handy in certain circumstances.

[1] [GitHub - AGWA/git-crypt: Transparent file encryption in git](https://github.com/AGWA/git-crypt)

---

<div class="post-metadata">

### Author: ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)
#### Post date: [January 25, 2020, 12:10am UTC](https://discourse.julialang.org/t/best-way-to-handle-user-constants-e-g-api-key/20912/5 "2020-01-25T00:10:37Z")

</div>

[https://github.com/JuliaIO/ConfParser.jl](https://github.com/JuliaIO/ConfParser.jl)

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [December 18, 2020, 10:01am UTC](https://discourse.julialang.org/t/best-way-to-handle-user-constants-e-g-api-key/20912/6 "2020-12-18T10:01:37Z")

</div>

@Tamas_Papp, any variables assigned in **startup.jl** seem to become constants (that cannot be changed) in the Julia session. Where in the Julia manual can we find this info?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [December 18, 2020, 10:12am UTC](https://discourse.julialang.org/t/best-way-to-handle-user-constants-e-g-api-key/20912/7 "2020-12-18T10:12:49Z")

</div>

I can’t replicate this, I can change variables defined there just fine (in 1.5 and 1.6).

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [December 18, 2020, 10:33am UTC](https://discourse.julialang.org/t/best-way-to-handle-user-constants-e-g-api-key/20912/8 "2020-12-18T10:33:36Z")

</div>

@Tamas_Papp, my apologies the question was incorrect. The error message was `ERROR: cannot declare str constant; it already has a value` , which seems to be the normal behavior. The variable ‘str’ was already assigned (in startup.jl) so it was not available to define a constant later.
