How to deloy API key to streamlit

I use environment varriable to API key. How to use them to deloy streamlit app

import requests, os, sys
import json
import streamlit as st
from PIL import Image

st.title('πŸš—πŸš•Nhan dang bien so xeπŸš—πŸš•')
image_file = st.file_uploader('Open Image', type=['PNG','JPG','BMP'])
col1, col2 = st.columns([5,2])
if image_file is not None:
    file_name =
    image_path = "./images/" + file_name
    image =
    with col1:
    model_id = os.environ.get('NANONETS_MODEL_ID')
    api_key = os.environ.get('NANONETS_API_KEY')

    url = '' + model_id + '/LabelFile/'

    data = {'file': open(image_path, 'rb'),    'modelId': ('', model_id)}

    response =, auth=requests.auth.HTTPBasicAuth(api_key, ''), files=data)

    model = json.loads(response.text)["result"][0]["prediction"][0]["ocr_text"]
    if st.button("Nhan dang"):
        with col2:
            st.markdown("### Bien so xe :")

The official recommendation is to use streamlit secrets Secrets management - Streamlit Docs instead of environment variables.

1 Like