---
title: "Setting Up Your API Key"
output: 
  rmarkdown::html_vignette:
    toc: true 
    toc_depth: 2
vignette: >
  %\VignetteIndexEntry{Setting Up Your API Key}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---
```{r setup, include=FALSE}
library(httptest2)
.mockPaths("../tests/mocks")
start_vignette(dir = "../tests/mocks")

original_options <- options("NIXTLA_API_KEY"="dummy_api_key", digits=7)

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>", 
  fig.width = 7, 
  fig.height = 4
)
```

```{r}
library(nixtlar)
```

This vignette explains what an API key is, how to get yours and how to set it up to use `nixtlar`. 

## 1. What is an API key? 
An API key is a unique string of characters that is used to authenticate your requests when using `nixtlar`. It is necessary to have a valid API key to use any of the core functions from `nixtlar` that interact with `TimeGPT`: 

```{r, eval=FALSE}
# core functions that interact with TimeGPT
- nixtlar::nixtla_client_forecast()
- nixtlar::nixtla_client_historic()
- nixtlar::nixtla_client_detect_anomalies()
- nixtlar::nixtla_client_cross_validation()
```

## 2. How can I get one? 
To obtain you API key, please sign up here: https://dashboard.nixtla.io/sign_in

After registering, you will have access to your developer dashboard. Under `API keys`, you will find your personal API key. Please note that your API key should not be shared with others, and it is your responsibility to keep it safe. 

## 3. How do I set up my API key? 

There are several methods to set up your API key. 

### 3.1 Using the `nixtlar::nixtla_set_api_key` function

`nixtlar` provides a function to directly set up your API key: 

```{r, eval=FALSE}
nixtlar::nixtla_set_api_key(api_key = "paste your API key here")
```

Keep in mind that if you close your R session or restart it, then you will need to set up your API key again.

### 3.2 Using an environment variable 

#### a. Using `options`

You can set up your API key using `options`. 

```{r, eval=FALSE}
options(NIXTLA_API_KEY="paste your API key here")
```

This will make your API key globally available throughout your R session. Although it will not appear in the list of variables, it will persist until you close or restart the session or until you explicitly change it.

To verify that it was set up correctly, use:

```{r, eval=FALSE}
getOption("NIXTLA_API_KEY")
```

#### b. Using `.Renviron` 

For a more persistent method that can be used across different projects, set up your API key as environment variable. To do this, you first need to load the `usethis` package.

```{r, eval=FALSE}
library(usethis)
usethis::edit_r_environ()
```

This will open your `.Reviron` file. Place your API key here, named `NIXTLA_API_KEY`.

```{r, eval=FALSE}
# Inside the .Renviron file 
NIXTLA_API_KEY="paste your API key here"
```

You will need to restart R for the changes to take effect. Note that modifying the `.Renviron` file affects **all** your R sessions, so if you are not comfortable with this, set your API key using the `nixtlar::nixtla_set_api_key function`.

![](../man/figures/diagram.png) 

## 4. Validate your API key (optional)

`nixtlar` includes a function to validate your API key.

```{r, eval=FALSE}
nixtlar::nixtla_validate_api_key()
```

The `nixtla_validate_api_key` function will return `TRUE` if your key is valid, and `FALSE` otherwise. 

You do not need to validate your API key every time you set it up, only when you are unsure of its status. Alternatively, in your dashboard, under `API keys`, there is a label next to your API key indicating its status, for example, `active`. 

```{r, include=FALSE}
options(original_options)
end_vignette()
```

