{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Setting up your API key"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "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](https://dashboard.nixtla.io/)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are different ways to set up your API key. We provide some examples below. A scematic is given below.\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"https://github.com/Nixtla/nixtla/blob/main/nbs/img/api_key_process.png?raw=true\" alt=\"Diagram of the API Key configuration process. Method 1. Unsecure. Copy API key from Nixtla dashboard. 2. Paste API Key in Python code. 3. Validate API key. Method 2. Secure. One method, temporary. Open terminal. Set environment variable. Validate API key. Another method, permanent. Create .env File. Set API Key in file. Validate API Key.\" />"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Copy and paste your key directly into your Python code\n",
    "\n",
    "This approach is straightforward and best for quick tests or scripts that won’t be shared.\n",
    "\n",
    "\n",
    "- **Step 1**: Copy the API key found in the `API Keys` of your [Nixtla dashboard](https://dashboard.nixtla.io/). \n",
    "- **Step 2**: Paste the key directly into your Python code, by instantiating the `NixtlaClient` with your API key:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from nixtla import NixtlaClient \n",
    "nixtla_client = NixtlaClient(api_key ='your API key here')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "::: {.callout-important}\n",
    "This approach is considered unsecure, as your API key will be part of your source code.\n",
    "::: "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Secure: using an environment variable"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **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.\n",
    "- **Step 2**: When you instantiate the `NixtlaClient` class, the SDK will automatically look for the `NIXTLA_API_KEY` environment variable and use it to authenticate your requests."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "::: {.callout-important}\n",
    "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.\n",
    "::: "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### a. Temporary: From the Terminal\n",
    "\n",
    "This approach is useful if you are working from a terminal, and need a temporary solution. \n",
    "\n",
    "#### Linux / Mac\n",
    "Open a terminal and use the `export` command to set `NIXTLA_API_KEY`. \n",
    "\n",
    "``` bash\n",
    "export NIXTLA_API_KEY=your_api_key\n",
    "```\n",
    "\n",
    "#### Windows\n",
    "For Windows users, open a Powershell window and use the `Set` command to set `NIXTLA_API_KEY`. \n",
    "\n",
    "``` powershell\n",
    "Set NIXTLA_API_KEY=your_api_key\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### b. Permanent: Using a `.env` file\n",
    "\n",
    "For 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:\n",
    "\n",
    "``` python\n",
    "NIXTLA_API_KEY=your_api_key\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "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:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from dotenv import load_dotenv\n",
    "load_dotenv()\n",
    "\n",
    "from nixtla import NixtlaClient\n",
    "nixtla_client = NixtlaClient()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "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."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "::: {.callout-important}\n",
    "Remember, your API key is like a password - keep it secret, keep it safe!\n",
    "::: "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Validate your API key\n",
    "\n",
    "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. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nixtla_client.validate_api_key()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "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](https://dashboard.nixtla.io/). "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
