API

class supervisely_lib.api.api.Api(server_address: str, token: str, retry_count: int = None, retry_sleep_sec: int = None, external_logger: logger = None, ignore_task_id: bool = False)[source]

Bases: object

An API connection to the server with which you can communicate with your teams, workspaces and projects. Api object is immutable.

Parameters
  • server_address (str) – Address of the server.

  • token (str) – Unique secret token associated with your agent.

  • retry_count (int, optional) – The number of attempts to connect to the server.

  • retry_sleep_sec (int, optional) – The number of seconds to delay between attempts to connect to the server.

  • external_logger (logger, optional) – Logger class object.

  • ignore_task_id (bool, optional) –

Raises

ValueError, if token is None or it length != 128

Usage example
# You can connect to API directly
address = 'https://app.supervise.ly/'
token = 'Your Supervisely API Token'
api = sly.Api(address, token)

# Or you can use API from environment
os.environ['SERVER_ADDRESS'] = 'https://app.supervise.ly'
os.environ['API_TOKEN'] = 'Your Supervisely API Token'
api = sly.Api.from_env()
classmethod from_env(retry_count: int = 5, ignore_task_id: bool = False)supervisely_lib.api.api.Api[source]

Initialize API use environment variables.

Parameters
  • retry_count (int) – The number of attempts to connect to the server.

  • ignore_task_id (bool) –

Returns

Api object

Return type

Api

Usage example
os.environ['SERVER_ADDRESS'] = 'https://app.supervise.ly'
os.environ['API_TOKEN'] = 'Your Supervisely API Token'
api = sly.Api.from_env()
add_header(key: str, value: str) → None[source]

Add given key and value to headers dictionary.

Parameters
  • key (str) – New key.

  • value (str) – New value.

Raises

RuntimeError, if key is already set

Returns

None

Return type

NoneType

add_additional_field(key: str, value: str) → None[source]

Add given key and value to additional_fields dictionary.

Parameters
  • key (str) – New key.

  • value (str) – New value.

Returns

None

Return type

NoneType

post(method: str, data: dict, retries: Optional[int] = None, stream: bool = False) → requests.models.Response[source]

Performs POST request to server with given parameters.

Parameters
  • method (str) –

  • data (dict) – Dictionary to send in the body of the Request.

  • retries (int, optional) – The number of attempts to connect to the server.

  • stream (bool, optional) – Define, if you’d like to get the raw socket response from the server.

Returns

Response object

Return type

Response

get(method: str, params: dict, retries: Optional[int] = None, stream: bool = False, use_public_api: bool = True) → requests.models.Response[source]

Performs GET request to server with given parameters.

Parameters
  • method (bool, optional) –

  • params – Dictionary to send in the body of the Request.

  • retries – The number of attempts to connect to the server.

  • stream – Define, if you’d like to get the raw socket response from the server.

  • use_public_api

Returns

Response object

Return type

Response

static parse_error(response: requests.models.Response, default_error: str = 'Error', default_message: str = 'please, contact administrator')[source]

Processes error from response.

Parameters
  • response – Request object.

  • default_error – Error description.

  • default_message – Message to user.

Returns

Number of error and message about curren connection mistake

Return type

int, str