mondras
09/06/2023, 11:52 PMMarvin
09/06/2023, 11:52 PMmondras
09/06/2023, 11:59 PMMarvin
09/07/2023, 12:00 AMmondras
09/07/2023, 12:05 AMMarvin
09/07/2023, 12:05 AMrequests
library:
python
import requests
import json
# Step 1: Delete the existing API key
delete_api_key_query = """
mutation($input: delete_api_key_input!) {
delete_api_key(input: $input) {
success
}
}
"""
delete_api_key_variables = {
"input": {
"key_id": "<EXISTING_API_KEY_ID>"
}
}
response = <http://requests.post|requests.post>(
'<https://api.prefect.io/graphql>',
headers={'Authorization': 'Bearer <YOUR_PREFECT_API_TOKEN>'},
json={'query': delete_api_key_query, 'variables': delete_api_key_variables}
)
print(json.loads(response.text))
# Step 2: Create a new API key
create_api_key_query = """
mutation($input: create_api_key_input!) {
create_api_key(input: $input) {
key {
id
key
}
}
}
"""
create_api_key_variables = {
"input": {
"user_id": "<YOUR_USER_ID>",
"name": "<NEW_API_KEY_NAME>"
}
}
response = <http://requests.post|requests.post>(
'<https://api.prefect.io/graphql>',
headers={'Authorization': 'Bearer <YOUR_PREFECT_API_TOKEN>'},
json={'query': create_api_key_query, 'variables': create_api_key_variables}
)
print(json.loads(response.text))
Replace <EXISTING_API_KEY_ID>
, <YOUR_PREFECT_API_TOKEN>
, <YOUR_USER_ID>
, and <NEW_API_KEY_NAME>
with your actual values.
This script first sends a mutation to delete the existing API key, then sends another mutation to create a new one.
Remember, humans, to handle your API keys with care. They're like the keys to your house, but for your software. If you lose them, you might find yourself locked out. Or worse, someone else might find them and get in. Isn't being a human fun?mondras
09/07/2023, 12:07 AMMarvin
09/07/2023, 12:07 AMmondras
09/07/2023, 12:18 AMmondras
09/07/2023, 12:18 AMMarvin
09/07/2023, 12:18 AMPOST /api/accounts/{account_id}/bots/{id}/rotate_api_key
endpoint to rotate the API key.
Here's a Python example using the requests
library:
python
import requests
account_id = "<YOUR_ACCOUNT_ID>"
bot_id = "<YOUR_BOT_ID>"
headers = {"Authorization": "Bearer <YOUR_PREFECT_API_TOKEN>"}
response = <http://requests.post|requests.post>(
f"<https://api.prefect.io/api/accounts/{account_id}/bots/{bot_id}/rotate_api_key>",
headers=headers
)
if response.status_code == 200:
print("API key rotated successfully.")
new_api_key = response.json()["key"]
print(f"New API key: {new_api_key}")
else:
print(f"Failed to rotate API key: {response.text}")
Replace <YOUR_ACCOUNT_ID>
, <YOUR_BOT_ID>
, and <YOUR_PREFECT_API_TOKEN>
with your actual values.
This script sends a POST request to the rotate API key endpoint. If the request is successful, it prints the new API key.
Remember, humans, to handle your API keys with care. They're like the keys to your house, but for your software. If you lose them, you might find yourself locked out. Or worse, someone else might find them and get in. Isn't being a human fun?