Patrick ODonnell
07/11/2025, 2:06 PMMarvin
07/11/2025, 2:06 PMMarvin
07/11/2025, 2:07 PMpython
from prefect.blocks.system import Secret
# Create a new secret
secret_block = Secret(value="my-secret-value")
secret_block.save(name="my-secret-name")
# Later, to use the secret in a flow:
secret = Secret.load("my-secret-name")
value = secret.get() # Returns "my-secret-value"
If you need to update an existing secret, you can use the overwrite
parameter:
python
secret_block = Secret(value="new-secret-value")
secret_block.save(name="my-secret-name", overwrite=True)
A few important notes:
1. The secret value is encrypted when stored
2. The name
parameter is what you'll use to reference this secret later in your flows
3. Use meaningful names that help identify the purpose of the secret
For more details and best practices, check out the secrets documentation.Patrick ODonnell
07/11/2025, 2:07 PMPatrick ODonnell
07/11/2025, 2:08 PMMarvin
07/11/2025, 2:08 PMMarvin
07/11/2025, 2:08 PMpython
from prefect.blocks.system import Secret
import json
# Your JSON data
json_data = {
"api_key": "secret-key",
"username": "user123",
"settings": {
"endpoint": "<https://api.example.com>",
"timeout": 30
}
}
# Convert the JSON to a string and create the secret
secret_block = Secret(value=json.dumps(json_data))
secret_block.save(name="my-json-secret")
# Later, to retrieve and use the JSON:
secret = Secret.load("my-json-secret")
json_value = json.loads(secret.get())
# Now you can access the JSON data
print(json_value["api_key"]) # Outputs: secret-key
print(json_value["settings"]["endpoint"]) # Outputs: <https://api.example.com>
A few important points:
1. The JSON object needs to be converted to a string using json.dumps()
before storing
2. When retrieving the secret, use json.loads()
to convert it back to a Python dictionary
3. The entire JSON object will be encrypted when stored
4. Make sure your JSON is valid and serializable before storing it
This approach works well for storing complex configuration objects that contain sensitive information. Just remember that the entire JSON object will be treated as sensitive data and encrypted.