MarcoM
11/14/2023, 2:11 PMMarvin
11/14/2023, 2:12 PM$ prefect config set PREFECT_API_URL="<https://api.prefect.cloud/api/accounts/[ACCOUNT-ID]/workspaces/[WORKSPACE-ID]>"
$ prefect config set PREFECT_API_KEY="[API-KEY]"
You can copy the PREFECT_API_URL
value directly from the page URL when you're in a Prefect Cloud workspace.
You can also set PREFECT_API_URL
and PREFECT_API_KEY
as you would any other environment variable.
For more details, you can refer to the Prefect documentation.MarcoM
11/14/2023, 2:23 PMMarvin
11/14/2023, 2:24 PMcreateVariable
mutation. Here is an example of how to do it using Python:
import requests
# Define the API endpoint
url = "<https://api.prefect.io/graphql>"
# Define the headers for the API request
headers = {
"Authorization": "Bearer YOUR_PREFECT_API_KEY",
"Content-Type": "application/json"
}
# Define the GraphQL mutation for creating a variable
mutation = """
mutation($input: createVariableInput!) {
createVariable(input: $input) {
id
}
}
"""
# Define the variables for the mutation
variables = {
"input": {
"name": "YOUR_VARIABLE_NAME",
"value": "YOUR_VARIABLE_VALUE"
}
}
# Make the API request
response = <http://requests.post|requests.post>(
url,
headers=headers,
json={
"query": mutation,
"variables": variables
}
)
# Print the response
print(response.json())
Replace YOUR_PREFECT_API_KEY
, YOUR_VARIABLE_NAME
, and YOUR_VARIABLE_VALUE
with your actual Prefect API key, variable name, and variable value respectively.
Please note that you need to have the necessary permissions to create variables in Prefect Cloud.
For more details, you can refer to the Prefect REST API documentation.MarcoM
11/14/2023, 2:27 PMMarvin
11/14/2023, 2:28 PMfrom prefect import variables
# from a synchronous context
answer = variables.get('the_answer')
print(answer) # 42
# from an asynchronous context
answer = await variables.get('the_answer')
print(answer) # 42
# without a default value
answer = variables.get('not_the_answer')
print(answer) # None
# with a default value
answer = variables.get('not_the_answer', default_value='42')
print(answer) # 42
For creating and managing variables via the REST API or CLI, please refer to the Prefect documentation.