Docs

Keeping Secrets

One of the great things about py.space is sharing your code! But you don’t want to share secret information like API keys or database passwords. So py.space has a concept of secrets. Secrets are strings that you can use in your code, but which other people can’t see in your code.

Setting up secrets

To edit your widget’s secrets, click on Secrets at the top right of the widget editor.

You’ll see a button, marked Create Secret. (If you’ve already got any secrets, you’ll see them too.)

Click Create Secret to create a new secret. You can then:

  • Click on a secret’s name to give it a new name – we’d recommend choosing something descriptive like api_key rather than new_secret_14.

  • Click to set a value for the secret. (This is where you actually enter the API key or password or whatever.)

  • Click to delete a secret.

  • Click to view the value of a secret. This is an emergency measure, in case you’ve forgotten something! (Don’t worry, this one isn’t available to people viewing your widget 😉)

Using secrets

To get the value of a secret in your code, first import anvil.secrets at the top of your code. Then call anvil.secrets.get_secret('api_key') (replacing api_key with)

Note: If you’re editing a GUI widget, you can only use secrets from server code – for security reasons, secrets aren’t available to client code.

Forking widgets with secrets

If you fork a widget, you get your own copy of its all its contents, right? Well, almost – you don’t get a copy of the widget’s secrets! Instead, all the secret names will be there, so you know what secrets the code is expecting, but all those secrets will be blank. You’ll need to use the button to set the value of each secret.

If you try to call get_secret(...) for a blank secret, you’ll get an error – so if you’ve cloned a widget that uses secrets, you’ll probably need to set new values for its secrets before you can use it.

Can you give me an example?

Sure! If you check out our Scheduled Task tutorial, the widget you build there uses a secret to hold the API key for a weather API.


Do you still have questions?

Our Community Forum is full of helpful information and friendly experts.