# SDK R

Il pacchetto `tomba` è il client R ufficiale per l'[API Tomba](/introduction). Ogni funzione accetta un oggetto client `Tomba` e invia una richiesta. Questa pagina descrive la versione 1.1.0, disponibile su GitHub.

## Requisiti

- R 3.5.0 o successivo, come dichiarato in `DESCRIPTION`.
- I pacchetti `httr` e `jsonlite`, installati come dipendenze.

## Installa

```r
install.packages("remotes")
remotes::install_github("tomba-io/r")
```

Su CRAN c'è la versione 1.0.1. Contiene solo `account`, `autocomplete`, `count`, `domain_search`, `email_finder`, `email_sources`, `email_verifier`, `logs`, `status` e `usage`, e queste funzioni restituiscono direttamente il corpo della risposta, senza il wrapper `data` e `rate_limit` descritto più avanti.

## Autenticati

Crea una chiave API e copia il tuo secret nella pagina [API keys](https://app.tomba.io/api), poi esportali entrambi, o impostali in `~/.Renviron`:

```bash
export TOMBA_API_KEY="ta_xxxx"
export TOMBA_SECRET_KEY="ts_xxxx"
```

Il pacchetto non legge da solo le variabili d'ambiente. Passa i valori come `Tomba(key = ..., secret = ...)`, e il client li invia come header `X-Tomba-Key` e `X-Tomba-Secret`. La scadenza e la rotazione delle chiavi sono trattate in [Autenticazione](/authentication).

## Prima richiesta

Trova l'indirizzo email di una persona in un'azienda:

```r
library(tomba)

client <- Tomba(key = Sys.getenv("TOMBA_API_KEY"), secret = Sys.getenv("TOMBA_SECRET_KEY"))

result <- email_finder(client, domain = "stripe.com", fname = "Jane", lname = "Doe")
print(result$data$data$email)
```

Ogni funzione restituisce una lista con due elementi: `data`, il corpo della risposta analizzato con `jsonlite::fromJSON`, e `rate_limit`. L'API racchiude il payload in un proprio oggetto `data` ([Formato della risposta](/introduction#response-format)), quindi l'indirizzo si trova in `result$data$data$email`.

## Errori

Quando l'API risponde con uno stato 400 o superiore, la funzione segnala un errore R il cui messaggio è `Tomba API error <status>: <response body>`. Intercettalo con `tryCatch`:

```r
library(tomba)

client <- Tomba(key = Sys.getenv("TOMBA_API_KEY"), secret = Sys.getenv("TOMBA_SECRET_KEY"))

result <- tryCatch(
  email_finder(client, domain = "stripe.com", fname = "Jane", lname = "Doe"),
  error = function(e) {
    message(conditionMessage(e))
    NULL
  }
)
if (!is.null(result)) print(result$data$data$email)
```

Il corpo della risposta nel messaggio è l'errore JSON, `{"errors":{"type":…,"message":…,"code":…}}`. Ogni `errors.type` è descritto in [Errori](/error-handling#error-types). `person_find`, `company_find` e `combined_find` non segnalano un errore quando non trovano nulla: questi endpoint rispondono con stato 200 e un corpo `errors`, quindi controlla `result$data$errors`.

## Limiti di frequenza e nuovi tentativi

Il pacchetto non riprova le richieste. `result$rate_limit` è una lista con nomi degli [header dei limiti di frequenza](/rate-limits#rate-limit-headers) presenti nella risposta, con valori character: `x-second-rate-limit`, `x-minute-rate-limit`, `x-daily-rate-limit`, `x-minute-request-left`, `x-daily-request-left`, `x-minute-reset-seconds`, `x-daily-reset-seconds`, `retry-after`, `ratelimit-policy` e `ratelimit`.

Un `429` segnala un errore senza gli header della risposta. Attendi prima di riprovare, come descritto in [Gestisci le risposte 429](/rate-limits#handle-429-responses).

Le richieste vanno in timeout dopo 120 secondi; il timeout non è configurabile.

## Riferimento dei metodi

Ogni funzione accetta il client come `obj`, il suo primo argomento.

| Funzione                                                               | Endpoint                                                                    |
| ---------------------------------------------------------------------- | --------------------------------------------------------------------------- |
| `account(obj)`                                                         | [`GET /me`](/api/account#get-account)                                       |
| `usage(obj)`                                                           | [`GET /usage`](/api/account#retrieve-api-usage)                             |
| `logs(obj, page = NULL, limit = NULL)`                                 | [`GET /logs`](/api/account#retrieve-api-logs)                               |
| `domain_search(obj, domain, enrich_mobile = NULL, webhook_url = NULL)` | [`GET /domain-search`](/api/finder#domain-search)                           |
| `email_finder(obj, domain, fname, lname, webhook_url = NULL)`          | [`GET /email-finder`](/api/finder#email-finder)                             |
| `author_finder(obj, url, webhook_url = NULL)`                          | [`GET /author-finder`](/api/finder#author-finder)                           |
| `linkedin_finder(obj, url, webhook_url = NULL)`                        | [`GET /linkedin`](/api/finder#linkedin-finder)                              |
| `email_format(obj, domain)`                                            | [`GET /email-format`](/api/finder#email-format)                             |
| `get_location(obj, domain)`                                            | [`GET /location`](/api/finder#location)                                     |
| `count(obj, domain)`                                                   | [`GET /email-count`](/api/finder#email-count)                               |
| `email_verifier(obj, email, webhook_url = NULL)`                       | [`GET /email-verifier`](/api/verifier#email-verifier)                       |
| `email_sources(obj, email)`                                            | [`GET /email-sources`](/api/email#email-sources)                            |
| `person_find(obj, email)`                                              | [`GET /people/find`](/api/enrichment#person-api)                            |
| `company_find(obj, domain)`                                            | [`GET /companies/find`](/api/enrichment#company-api)                        |
| `combined_find(obj, email)`                                            | [`GET /combined/find`](/api/enrichment#combined-api)                        |
| `phone_finder(obj, email, webhook_url = NULL)`                         | [`GET /phone-finder`](/api/phone#phone-finder)                              |
| `phone_validator(obj, phone, country_code = NULL)`                     | [`GET /phone-validator`](/api/phone#phone-validator)                        |
| `status(obj, domain)`                                                  | [`GET /domain-status`](/api/domain#domain-status)                           |
| `autocomplete(obj, search)`                                            | [`GET /domain-suggestions`](/api/domain-suggestions#get-domain-suggestions) |
| `similar_domains(obj, domain)`                                         | [`GET /similar`](/api/domain#similar)                                       |
| `technology_check(obj, domain)`                                        | [`GET /technology`](/api/domain#technology)                                 |
| `companies_search(obj, data = list())`                                 | [`POST /reveal/search`](/api/reveal#search-companies)                       |
| `list_flags(obj, page = NULL, limit = NULL)`                           | [`GET /flag`](/api/flag#list-my-flags)                                      |
| `list_keys(obj)`                                                       | [`GET /keys`](/api/keys#retrieve-api-keys)                                  |
| `get_key(obj, id)`                                                     | [`GET /keys/{id}`](/api/keys#retrieve-a-single-api-key)                     |
| `create_key(obj, data = list())`                                       | [`POST /keys`](/api/keys#create-an-api-key)                                 |
| `reset_key(obj, id)`                                                   | [`PUT /keys/{id}`](/api/keys#reset-an-api-key)                              |
| `delete_key(obj, id)`                                                  | [`DELETE /keys/{id}`](/api/keys#delete-an-api-key)                          |
| `list_leads(obj, page = NULL, limit = NULL, domain = NULL)`            | [`GET /leads`](/api/leads#retrieve-leads)                                   |
| `get_lead(obj, lead_id)`                                               | [`GET /leads/{id}`](/api/leads#retrieve-a-single-lead)                      |
| `create_lead(obj, data = list())`                                      | [`POST /leads`](/api/leads#create-a-lead)                                   |
| `update_lead(obj, lead_id, data = list())`                             | [`PUT /leads/{id}`](/api/leads#update-a-lead)                               |
| `delete_lead(obj, lead_id)`                                            | [`DELETE /leads/{id}`](/api/leads#delete-a-lead)                            |
| `list_leads_lists(obj)`                                                | [`GET /leads_lists`](/api/lead-lists#retrieve-leads-lists)                  |
| `create_leads_list(obj, data = list())`                                | [`POST /leads_lists`](/api/lead-lists#create-leads-list)                    |
| `update_leads_list(obj, id, data = list())`                            | [`PUT /leads_lists/{id}`](/api/lead-lists#update-leads-list)                |
| `delete_leads_list(obj, id)`                                           | [`DELETE /leads_lists/{id}`](/api/lead-lists#delete-leads-list)             |
| `list_lead_attributes(obj)`                                            | [`GET /attributes`](/api/lead-attributes#retrieve-lead-attributes)          |
| `create_lead_attribute(obj, data = list())`                            | [`POST /attributes`](/api/lead-attributes#create-a-lead-attribute)          |
| `update_lead_attribute(obj, id, data = list())`                        | [`PUT /attributes/{id}`](/api/lead-attributes#update-a-lead-attribute)      |
| `delete_lead_attribute(obj, id)`                                       | [`DELETE /attributes/{id}`](/api/lead-attributes#delete-a-lead-attribute)   |
| `list_bulks(obj, bulk_type, page = NULL, limit = NULL)`                | [`GET /bulk/{type}`](/api/bulks)                                            |
| `create_bulk(obj, bulk_type, data = list())`                           | [`POST /bulk/{type}`](/api/bulks)                                           |
| `get_bulk(obj, bulk_type, bulk_id)`                                    | [`GET /bulk/{type}/{id}`](/api/bulks)                                       |
| `launch_bulk(obj, bulk_type, bulk_id)`                                 | [`PUT /bulk/{type}/{id}`](/api/bulks)                                       |
| `bulk_progress(obj, bulk_type, bulk_id)`                               | [`GET /bulk/{type}/{id}/progress`](/api/bulks)                              |
| `rename_bulk(obj, bulk_type, bulk_id, name)`                           | [`PUT /bulk/{type}/{id}/rename`](/api/bulks)                                |
| `archive_bulk(obj, bulk_type, bulk_id)`                                | [`DELETE /bulk/{type}/{id}/archive`](/api/bulks)                            |
| `delete_bulk(obj, bulk_type, bulk_id)`                                 | [`DELETE /bulk/{type}/{id}/delete`](/api/bulks)                             |

`bulk_type` è il segmento `{type}` elencato in [Tipi di job in blocco](/bulks#bulk-types); l'ordine delle chiamate è descritto in [Ciclo di vita](/bulks#lifecycle). `create_bulk` invia un corpo JSON, quindi non può creare job che richiedono il caricamento di un file CSV, come i job [Email Finder](/bulks#email-finder).

## Codice sorgente

[github.com/tomba-io/r](https://github.com/tomba-io/r) (Apache-2.0).
