Deploying a REST API

Anaconda Enterprise enables you to deploy your machine learning or predictive models as a REST API endpoint so others can query and consume results from them. REST APIs are web server endpoints, or callable URLs, which provide results based on a query, allowing developers to create applications that programmatically query and consume them via other user interfaces or applications.

Rather than sharing your model with other data scientists and having them run it, you can give them an endpoint to query the model, which you can continue to update, improve and redeploy as needed.

REST API endpoints deployed with Anaconda Enterprise are secure and only accessible to users that you’ve shared the deployment with or users that have generated a token that can be used to query the REST API endpoint outside of Anaconda Enterprise.

The process of deploying a REST API involves the following steps:

  • Create a project to encapsulate all of the components necessary to use or run your model.
  • Deploy the project with the rest_api command (shown in Step 4 below) to build the software dependencies—all of the libraries required for it to run—and encapsulate them so they are completely self-contained.
  • Share the deployment, or the URL of the endpoint, and generate a unique token so that they can connect to the deployment and use it from within notebooks, APIs or other applications.

Using the API wrapper

As an alternative to using the REST API wrapper provided with Anaconda Enterprise, you can construct an API endpoint using any web framework and serve the endpoint on port 8086 within your deployment, to make it available as a secure REST API endpoint in Anaconda Enterprise.

Follow this process to wrap your code with an API:

  1. Open the Jupyter Notebook and add this code to be able to handle HTTP requests. Define a global REQUEST JSON string that will be replaced on each invocation of the API.

    import json
    REQUEST = json.dumps({
        'path' : {},
        'args' : {},
        'body': {}
    })
    
  2. Import the Anaconda Enterprise publish function.

    from anaconda_enterprise import publish
    @publish(methods=['GET', 'POST'])
    def function():
        ...
        return json.dumps(...)
    
  3. Add the deployment command and an appropriate environment variable to the anaconda-project.yml file:

    commands:
      deploy-api:
        rest_api: {notebook}.ipynb
        supports_http_options: true
        default: true
    
    variables:
       KG_FORCE_KERNEL_NAME:
        default: python3
    
  4. Ensure the anaconda-enterprise channel is listed under channels: and the anaconda-enterprise-web-publisher package is listed under packages:. For example:

    packages:
      - python=3.6
      - pandas
      - dask
      - matplotlib
      - scikit-learn
      - requests
      - anaconda-enterprise-web-publisher
    
    channels:
      - defaults
      - anaconda-enterprise
    
  5. Use the following command to test the API within your notebook session (without deploying it):

    anaconda-project run deploy-api
    
  6. Now if you visit http://localhost:8888/{function} from within a notebook session you will see the results of your function.

    From within a notebook session, execute the following command:

    curl localhost:8888/{function}
    
  7. Click the Deploy icon in the toolbar to deploy the project as an API.

    This deploys the notebook as an API which you can then query.

  8. To query externally, create a token and find the url to the running project.

    Example using curl:

    export TOKEN="<generated-token-goes-here>"  # save long string of text in variable
    curl -L -H "Authorization: Bearer $TOKEN" <url-of-project>
    

    The -L option tells curl to follow redirects. The -H adds a header. In this case -H adds the token required to authorize the client to visit that URL.

    If you deploy the project as described above you can add the -X POST option to curl to access that function.