Setting up your API key
This tutorial will explain how to set up your API key when using the Nixtla SDK. To create an Api Key
go to your Dashboard.
There are different ways to set up your API key. We provide some examples below. A scematic is given below.
1. Copy and paste your key directly into your Python code
This approach is straightforward and best for quick tests or scripts that won’t be shared.
- Step 1: Copy the API key found in the
API Keys
of your Nixtla dashboard. - Step 2: Paste the key directly into your Python code, by instantiating the
NixtlaClient
with your API key:
from nixtla import NixtlaClient
nixtla_client = NixtlaClient(api_key ='your API key here')
Important
This approach is considered unsecure, as your API key will be part of your source code.
2. Secure: using an environment variable
- Step 1: Store your API key in an environment variable named
NIXTLA_API_KEY
. This can be done (a) temporarily for a session or (b) permanently, depending on your preference. - Step 2: When you instantiate the
NixtlaClient
class, the SDK will automatically look for theNIXTLA_API_KEY
environment variable and use it to authenticate your requests.
Important
The environment variable must be named exactly
NIXTLA_API_KEY
, with all capital letters and no deviations in spelling, for the SDK to recognize it.
a. Temporary: From the Terminal
This approach is useful if you are working from a terminal, and need a temporary solution.
Linux / Mac
Open a terminal and use the export
command to set NIXTLA_API_KEY
.
export NIXTLA_API_KEY=your_api_key
Windows
For Windows users, open a Powershell window and use the Set
command to set NIXTLA_API_KEY
.
Set NIXTLA_API_KEY=your_api_key
b. Permanent: Using a .env
file
.env
fileFor a more persistent solution place your API key in a .env
file located in the folder of your Python script. In this file, include the following:
NIXTLA_API_KEY=your_api_key
You can now load the environment variable within your Python script. Use the dotenv
package to load the .env
file and then instantiate the NIXTLA_API_KEY
class. For example:
from dotenv import load_dotenv
load_dotenv()
from nixtla import NixtlaClient
nixtla_client = NixtlaClient()
This approach is more secure and suitable for applications that will be deployed or shared, as it keeps API keys out of the source code.
Important
Remember, your API key is like a password - keep it secret, keep it safe!
3. Validate your API key
You can always find your API key in the API Keys
section of your dashboard. To check the status of your API key, use the validate_api_key
method of the NixtlaClient
class. This method will return True
if the API key is valid and False
otherwise.
nixtla_client.validate_api_key()
You don’t need to validate your API key every time you use TimeGPT
. This function is provided for your convenience to ensure its validity. For full access to TimeGPT
’s functionalities, in addition to a valid API key, you also need sufficient credits in your account. You can check your credits in the Usage
section of your dashboard.
Updated 3 months ago