Authentication

Before making API calls, we need to define how those calls are going to be authenticated. Currently, we support two different authentication methods: unauthenticated and API key based.

Therefore, we first need to create an authentication client that will be used when instantiating the Python classes that deal with API requests.

For unauthenticated requests, we need to create a NoAuthClient object:

1
2
3
4
5
6
  from carto.auth import NoAuthClient

  USERNAME="type here your username"
  USR_BASE_URL = "https://{user}.carto.com/".format(user=USERNAME)
  auth_client = NoAuthClient(base_url=USR_BASE_URL)

For API key authenticated requests, we need to create an APIKeyAuthClient instance:

1
2
3
4
5
6
  from carto.auth import APIKeyAuthClient

  USERNAME="type here your username"
  USR_BASE_URL = "https://{user}.carto.com/".format(user=USERNAME)
  auth_client = APIKeyAuthClient(api_key="myapikey", base_url=USR_BASE_URL)

API key is mandatory for all API requests except for sending SQL queries to public datasets.

Tip: If you want to learn more about authentication and authorization, read the fundamentals about this topic, or dig into the Auth API details.

The base_url parameter must include the user and or the organization with a format similar to these ones:

1
2
3
4
5
  BASE_URL = "https://{organization}.carto.com/user/{user}/". \
      format(organization=ORGANIZATION,
             user=USERNAME)
  USR_BASE_URL = "https://{user}.carto.com/".format(user=USERNAME)

For a detailed description of the rest of parameters both constructors accept, please take a look at the reference.

Finally, you can use a NonVerifiedAPIKeyAuthClient instance if you are running CARTO on your premises and don’t have a valid SSL certificate:

1
2
3
4
5
6
7
  from carto.auth import NonVerifiedAPIKeyAuthClient

  USERNAME="type here your username"
  YOUR_ON_PREM_DOMAIN="myonprem.com"
  USR_BASE_URL = "https://{domain}/user/{user}".format(domain=YOUR_ON_PREM_DOMAIN, user=USERNAME)
  auth_client = NonVerifiedAPIKeyAuthClient(api_key="myapikey", base_url=USR_BASE_URL)