ambient_api icon indicating copy to clipboard operation
ambient_api copied to clipboard

How do I set the API key in the script?

Open Viss opened this issue 5 years ago • 2 comments

I'd like to be able to set the API key in the script itself versus using environment variables, making it a bit more portable could you provide an example of how to do that?

Viss avatar Feb 22 '20 00:02 Viss

You can pass it in as a Keyword argument.

api = AmbientAPI(AMBIENT_API_KEY="your key here")

avryhof avatar Feb 24 '20 23:02 avryhof

I ended up using a json file to keep all of my values with the example below:

contents of AUTH.json:

{
    "AMBIENT_APPLICATION_KEY":"KEY_HEREEE",
    "AMBIENT_API_KEY":"KEY HERE",
    "AMBIENT_ENDPOINT":"https://rt.ambientweather.net/v1",
    "log_level": "debug"
}

ex.py script:

from ambient_api.ambientapi import AmbientAPI
import time, json
from pathlib import Path
import inspect

CURRENT_FILE_PATH = Path(inspect.getfile(lambda: None)).parent

LOG_FILE = CURRENT_FILE_PATH / "data.log"

with open(CURRENT_FILE_PATH / "AUTH.json") as fd:
    auth = json.load(fd)
    
api = AmbientAPI(**auth)

devices = api.get_devices()

device = devices[0]
time.sleep(1)
print(device.get_data())

kyrlon avatar Jun 16 '24 03:06 kyrlon