# Lua-SDK

De rock `tomba` is de officiële Lua-client voor de [Tomba API](/introduction). Alle methoden zitten op één clientobject, en elke methode stuurt één verzoek.

## Vereisten

- Lua 5.1, 5.2 of 5.3. De rockspec staat Lua 5.1 of hoger toe, maar de rock `xml` waarvan `lua-requests` afhankelijk is, vereist een versie ouder dan 5.4.
- [LuaRocks](https://luarocks.org/) en een C++-compiler, om de afhankelijkheden `lua-requests` en `lua-cjson` te bouwen.

## Installeren

```bash
luarocks install xml CFLAGS="-O2 -fPIC -std=gnu++14"
luarocks install tomba
```

Het eerste commando bouwt de rock `xml` in de modus C++14. Zonder dat commando mislukt `luarocks install tomba` op compilers die standaard C++17 gebruiken, zoals GCC 11 en hoger, omdat `xml` niet compileert als C++17.

## Authenticeren

Maak een API-sleutel aan en kopieer je secret op de pagina [API keys](https://app.tomba.io/api), en exporteer daarna beide:

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

De SDK leest geen omgevingsvariabelen. Geef de waarden door aan `Tomba:init(key, secret)`, en de client stuurt ze mee als de headers `X-Tomba-Key` en `X-Tomba-Secret`. `init` geeft een fout als een van beide waarden korter is dan 39 tekens. Het verlopen en roteren van sleutels staat in [Authenticatie](/authentication).

## Eerste verzoek

Zoek het e-mailadres van een persoon bij een bedrijf op:

```lua
local Tomba = require("tomba")

local tomba = Tomba:init(os.getenv("TOMBA_API_KEY"), os.getenv("TOMBA_SECRET_KEY"))

local result = tomba:email_finder("stripe.com", "Jane", "Doe")
print(result.data.data.email)
```

Elke methode geeft een tabel terug met twee velden: `data`, de gedecodeerde JSON-body, en `rate_limit`. De API verpakt de payload in een eigen object `data` ([Antwoordformaat](/introduction#response-format)), dus het adres staat in `result.data.data.email`. JSON-waarden `null` worden gedecodeerd naar `cjson.null`.

## Fouten

Hoe een fout zich voordoet, hangt af van de HTTP-methode:

- `GET`-methoden geven een Lua-fout als de status niet 200 is. Het foutbericht is de onbewerkte body van het antwoord, voorafgegaan door het bestand en het regelnummer van de SDK.
- `POST`-, `PUT`- en `DELETE`-methoden geven geen fout bij API-fouten. Ze geven voor elke status de resultaattabel terug, dus controleer `result.data.errors`.

Vang fouten van `GET` op met `pcall` en decodeer de body:

```lua
local Tomba = require("tomba")
local cjson = require("cjson")

local tomba = Tomba:init(os.getenv("TOMBA_API_KEY"), os.getenv("TOMBA_SECRET_KEY"))

local ok, result = pcall(tomba.email_finder, tomba, "stripe.com", "Jane", "Doe")
if ok then
    print(result.data.data.email)
else
    -- The error message is the response body, prefixed with the SDK's file and line.
    local decoded, body = pcall(cjson.decode, tostring(result):match("{.*") or "")
    if decoded and type(body) == "table" and body.errors then
        print(body.errors.code, body.errors.type, body.errors.message)
    else
        print(result)
    end
end
```

`errors.code` is de HTTP-status. Elk `errors.type` wordt beschreven in [Fouten](/error-handling#error-types).

## Rate limits en nieuwe pogingen

De SDK probeert niets opnieuw. `result.rate_limit` bevat de [rate-limitheaders](/rate-limits#rate-limit-headers) van het antwoord die aanwezig zijn, met de headernaam als sleutel en stringwaarden: `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` en `ratelimit`.

Een `429` bij een `GET`-methode geeft een fout zonder de antwoordheaders. Wacht voordat je het opnieuw probeert, zoals beschreven in [429-antwoorden afhandelen](/rate-limits#handle-429-responses).

Verzoeken krijgen na 120 seconden een time-out; de time-out is niet instelbaar.

## Naslag van methoden

Roep elke methode op de client aan met een dubbele punt, bijvoorbeeld `tomba:email_finder(...)`. `opts` is een optionele tabel.

| Methode                                                                          | Endpoint                                                                        |
| ------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- |
| `account()`                                                                     | [`GET /me`](/api/account#get-account)                                           |
| `usage()`                                                                       | [`GET /usage`](/api/account#retrieve-api-usage)                                 |
| `logs(params)`                                                                  | [`GET /logs`](/api/account#retrieve-api-logs)                                   |
| `domain_search(domain, opts)`, met `opts.enrich_mobile` en `opts.webhook_url` | [`GET /domain-search`](/api/finder#domain-search)                               |
| `email_finder(domain, first_name, last_name, opts)`, met `opts.webhook_url`    | [`GET /email-finder`](/api/finder#email-finder)                                 |
| `author_finder(url, opts)`                                                      | [`GET /author-finder`](/api/finder#author-finder)                               |
| `linkedin_finder(url, opts)`                                                    | [`GET /linkedin`](/api/finder#linkedin-finder)                                  |
| `enrichment(email, opts)`                                                       | [`GET /enrich`](/api/finder#email-enrichment)                                   |
| `email_format(domain)`                                                          | [`GET /email-format`](/api/finder#email-format)                                 |
| `get_location(domain)`                                                          | [`GET /location`](/api/finder#location)                                         |
| `count(domain)`                                                                 | [`GET /email-count`](/api/finder#email-count)                                   |
| `email_verifier(email, opts)`                                                   | [`GET /email-verifier`](/api/verifier#email-verifier)                           |
| `email_sources(email)`                                                          | [`GET /email-sources`](/api/email#email-sources)                                |
| `person_find(email)`                                                            | [`GET /people/find`](/api/enrichment#person-api)                                |
| `company_find(domain)`                                                          | [`GET /companies/find`](/api/enrichment#company-api)                            |
| `combined_find(email)`                                                          | [`GET /combined/find`](/api/enrichment#combined-api)                            |
| `phone_finder(params)`                                                          | [`GET /phone-finder`](/api/phone#phone-finder)                                  |
| `phone_validator(phone, country_code)`                                          | [`GET /phone-validator`](/api/phone#phone-validator)                            |
| `status(domain)`                                                                | [`GET /domain-status`](/api/domain#domain-status)                               |
| `autocomplete(search)`                                                          | [`GET /domain-suggestions`](/api/domain-suggestions#get-domain-suggestions)     |
| `similar(domain)`                                                               | [`GET /similar`](/api/domain#similar)                                           |
| `technology(domain)`                                                            | [`GET /technology`](/api/domain#technology)                                     |
| `companies_search(body)`                                                        | [`POST /reveal/search`](/api/reveal#search-companies)                           |
| `list_flags(params)`                                                            | [`GET /flag`](/api/flag#list-my-flags)                                          |
| `create_flag(body)`                                                             | [`POST /flag`](/api/flag#flag-incorrect-data)                                   |
| `list_keys()`                                                                   | [`GET /keys`](/api/keys#retrieve-api-keys)                                      |
| `get_key(key_id)`                                                               | [`GET /keys/{id}`](/api/keys#retrieve-a-single-api-key)                         |
| `create_key()`                                                                  | [`POST /keys`](/api/keys#create-an-api-key)                                     |
| `reset_key(key_id)`                                                             | [`PUT /keys/{id}`](/api/keys#reset-an-api-key)                                  |
| `delete_key(key_id)`                                                            | [`DELETE /keys/{id}`](/api/keys#delete-an-api-key)                              |
| `list_leads(params)`                                                            | [`GET /leads`](/api/leads#retrieve-leads)                                       |
| `get_lead(lead_id)`                                                             | [`GET /leads/{id}`](/api/leads#retrieve-a-single-lead)                          |
| `create_lead(body)`                                                             | [`POST /leads`](/api/leads#create-a-lead)                                       |
| `update_lead(lead_id, body)`                                                    | [`PUT /leads/{id}`](/api/leads#update-a-lead)                                   |
| `delete_lead(lead_id)`                                                          | [`DELETE /leads/{id}`](/api/leads#delete-a-lead)                                |
| `list_leads_lists()`                                                            | [`GET /leads_lists`](/api/lead-lists#retrieve-leads-lists)                      |
| `get_leads_list(list_id)`                                                       | [`GET /leads_lists/{id}`](/api/lead-lists#retrieve-a-single-leads-list)         |
| `create_leads_list(body)`                                                       | [`POST /leads_lists`](/api/lead-lists#create-leads-list)                        |
| `update_leads_list(list_id, body)`                                              | [`PUT /leads_lists/{id}`](/api/lead-lists#update-leads-list)                    |
| `delete_leads_list(list_id)`                                                    | [`DELETE /leads_lists/{id}`](/api/lead-lists#delete-leads-list)                 |
| `list_attributes()`                                                             | [`GET /attributes`](/api/lead-attributes#retrieve-lead-attributes)              |
| `get_attribute(attribute_id)`                                                   | [`GET /attributes/{id}`](/api/lead-attributes#retrieve-a-single-lead-attribute) |
| `create_attribute(body)`                                                        | [`POST /attributes`](/api/lead-attributes#create-a-lead-attribute)              |
| `update_attribute(attribute_id, body)`                                          | [`PUT /attributes/{id}`](/api/lead-attributes#update-a-lead-attribute)          |
| `delete_attribute(attribute_id)`                                                | [`DELETE /attributes/{id}`](/api/lead-attributes#delete-a-lead-attribute)       |
| `list_bulks(bulk_type, params)`                                                 | [`GET /bulk/{type}`](/api/bulks)                                                |
| `create_bulk(bulk_type, data)`                                                  | [`POST /bulk/{type}`](/api/bulks)                                               |
| `get_bulk(bulk_type, id)`                                                       | [`GET /bulk/{type}/{id}`](/api/bulks)                                           |
| `launch_bulk(bulk_type, id)`                                                    | [`PUT /bulk/{type}/{id}`](/api/bulks)                                           |
| `bulk_progress(bulk_type, id)`                                                  | [`GET /bulk/{type}/{id}/progress`](/api/bulks)                                  |
| `bulk_download(bulk_type, id)`                                                  | [`GET /bulk/{type}/{id}/download`](/api/bulks)                                  |
| `rename_bulk(bulk_type, id, name)`                                              | [`PUT /bulk/{type}/{id}/rename`](/api/bulks)                                    |
| `archive_bulk(bulk_type, id)`                                                   | [`DELETE /bulk/{type}/{id}/archive`](/api/bulks)                                |
| `delete_bulk(bulk_type, id)`                                                    | [`DELETE /bulk/{type}/{id}/delete`](/api/bulks)                                 |

`bulk_type` is het `{type}`-segment uit [Bulktypes](/bulks#bulk-types); de volgorde van de aanroepen staat in [Levenscyclus](/bulks#lifecycle). `create_bulk` stuurt een JSON-body, dus het kan geen taken aanmaken waarvoor een CSV-upload nodig is, zoals [Email Finder-taken](/bulks#email-finder).

## Broncode

[github.com/tomba-io/lua](https://github.com/tomba-io/lua), in de rockspec gelicentieerd onder Apache-2.0.
