Renu Dhaka
Renu Dhaka

Reputation: 21

How to use API Key without directly using it in the Python code?

I want get data using HubSpot API in Python, however, I don't want to show my ApiKey in the code. What would be the best practice to get data using API Without showing your API key? I am currently working in Jupyter notebook.

Upvotes: 2

Views: 6290

Answers (2)

Bao Dinh
Bao Dinh

Reputation: 84

https://www.youtube.com/watch?v=YdgIWTYQ69A u can flow this tutorial

Simply create a file name ".env" and write your API key in Like this : "API_KEY = YOUR_API_KEY"

then run "pip3 install python-dotenv" in your cmd

then in your code, add some lines :

from dotenv import load_dotenv
import os

load_dotenv()

API_KEY=os.getenv("API_KEY")

Upvotes: 4

ElliotSknr
ElliotSknr

Reputation: 326

You would be looking to store the API key in your environment variables, then read from the environment variables in your python code.

In Jupyter you could achieve this with magic commands; https://ipython.readthedocs.io/en/stable/interactive/magics.html

Set with

%env VAR_KEY VAR_VALUE

You can then use these with

import os
os.getenv(key)

Example

Alternatively, instead of magic commands you could set the env variable in your kernel.json, see here; https://stackoverflow.com/a/53595397/12707704

Upvotes: 2

Related Questions