0
0
Prompt Engineering / GenAIml~10 mins

API key management in Prompt Engineering / GenAI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to load the API key from an environment variable.

Prompt Engineering / GenAI
import os
api_key = os.getenv([1])
Drag options to blanks, or click blank then click option'
A"api_key"
BAPI_KEY
C"API_KEY"
Dapi_key
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the environment variable name.
2fill in blank
medium

Complete the code to check if the API key is missing and raise an error.

Prompt Engineering / GenAI
if [1] is None:
    raise ValueError("API key is missing")
Drag options to blanks, or click blank then click option'
Aapi_key is None
Bapi_key
Capi_key == ''
Dnot api_key
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of 'is' to check None.
3fill in blank
hard

Fix the error in the code to securely store the API key in a variable.

Prompt Engineering / GenAI
def store_key():
    [1] = os.getenv("API_KEY")
    return api_key
Drag options to blanks, or click blank then click option'
Aapi_key
BAPI_KEY
Ckey
DapiKey
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name than the one returned.
4fill in blank
hard

Fill both blanks to create a function that validates the API key length and raises an error if invalid.

Prompt Engineering / GenAI
def validate_key(key):
    if len(key) [1] 32:
        raise ValueError("Invalid API key length")
    return True
Drag options to blanks, or click blank then click option'
A==
B>
C!=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' for length check.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps API key names to their masked versions if the key length is valid.

Prompt Engineering / GenAI
masked_keys = [1]: key[:4] + '****' + key[-4:] for [2], key in keys.items() if len(key) [3] 32
Drag options to blanks, or click blank then click option'
A{name
Bname
C<=
Dkey
Attempts:
3 left
💡 Hint
Common Mistakes
Missing curly braces in comprehension.
Using wrong comparison operator.