API Protect Python SDK Installation and Usage

Overview

The Data Theorem API Protect Python SDK is a Python library that provides protection for APIs written in Python.

The SDK integrates with your Python API using the API Protect middleware which supports most popular Python frameworks, including:

To Protect an API, all the API developer needs to do is add the API Protect middleware to their web application code set the client Id we provide.

Getting the Software

Use the link provided to you to download the API Protect software bundle which contains:

1. apiprotect Python package

2. a file named env that contains your client_id

Note: To get a download link contact sales or support.

Example downloading and extracting the software bundle

$ > curl https://<pre-signed-gcs-url>/apiprotect-bundle.tar.gz | tar -x $ > ls apiprotect-1.0.3.tar.gz env $ > cat env DT_API_PROTECT_CLIENT_ID=ca1c3cf9a87b9018e2c5a8f2f1096c3d41eda70ea918bc76b0f3d7a22c224710

Installation

Step 1: Add the package to dependencies

To install API Protect the provided Python package must be added as a dependency, typically by adding it to a requirements.txt file. Here is an example of how to add the package:

Example adding dependency to requirements.txt

# requirements.txt after adding the apiprotect package file:./vendored/apiprotect-1.0.3.tar.gz Jinja2<2.12 SQLAlchemy<1.4.0 advocate>=1.0.0,<2.0 boto3<2.0.0 falcon-cors<1.2.0 falcon<4.0.0

Example installing dependency via pip

$ > pip install ./apiprotect-1.0.3.tar.gz

Step 2: Add the middleware to the application

The middleware for your web framework should be imported and added to your application.

Note: The ordering of middleware is important

Example using Falcon middleware

Example using WSGI middleware with Flask

Example using the WSGI middleware with Django

Step 3: Set the Client ID

For each API you protect Data Theorem provides a unique identifier which authenticates the SDK when it communicates with our services.

The client id we provide can be set in two way, either:

  • via an environment variable DT_API_PROTECT_CLIENT_ID

  • or by passing it directly to the middleware. Setting the environment variable is the preferred method.

Note: If the client id is present either as the DT_API_PROTECT_CLIENT_ID environment variable, or passed to the middleware, API Protect will activate. If the client_id cannot be easily unset but you do not wish to activate API Protect, you can set DT_API_PROTECT_DEACTIVATE=True to prevent the service from activating.

If you need to pass the client_id to the middleware without setting an environment variable, here is how to do that:

Example passing client_id to Falcon middleware

Example passing client_id to WSGI middleware