This guide will cover setting up the client, calling an algorithm using direct user input, and calling an algorithm that uses JSON as input. For complete details about the Algorithmia API, please refer to the API Docs.
Set Up the Client
To use the client you’ll need an API key, which Algorithmia uses for fine-grained authentication across the platform. For this example, we’ll use the
default-key that was created along with your account, which has a broad set of permissions. Log in to Algorithmia and navigate to Home > API Keys to find your key, or read the API keys documentation for more information.
Once the client is included, you can instantiate the client object:
Specifying an On-Premises or Private Cloud Endpoint
This feature is available to Algorithmia Enterprise users only.
If you are running Algorithmia Enterprise, you can specify the API endpoint when you create the client object:
Calling an Algorithm
Algorithms take three basic types of input whether they are invoked directly through the API or by using a client library: strings, JSON, and binary data. In addition, individual algorithms might have their own I/O requirements, such as using different data types for input and output, or accepting multiple types of input, so consult the input and output sections of an algorithm’s documentation for specifics.
The first algorithm we’ll call is a demo version of the algorithm used in the Algorithm Development Getting Started guide, which is available at demo/Hello. Looking at the algorithm’s documentation, it takes a string as input and returns a string.
Then, we can use the
.then() to handle the result and any errors.
Which should print the phrase
Hello HAL 9000 to the console.
Let’s look at an example using JSON and the nlp/LDA algorithm. The algorithm docs tell us that the algorithm takes a list of documents and returns a number of topics that are relevant to those documents. The documents can be a list of strings, a Data API file path, or a URL. We’ll call this algorithm using a JSON object as our input, following the format in the algorithm documentation:
The output will be a JSON object which includes an array of topics which include relevant words and the number of occurrences.
You might have noticed that in this example we included a version number when instantiating the algorithm. Pinning your code to a specific version of the algorithm can be especially important in a production environment where the underlying implementation might change from version to version.
.pipe() returning an error object in their output.
Your account can make up to 80 Algorithmia requests at the same time (this limit can be raised if needed).
Algorithm requests have a payload size limit of 10MB for input and 15MB for output. If you need to work with larger amounts of data, you can make use of the Algorithmia Data API.
Note: Working with Files
For larger files, you can set up an Amazon S3 Connector to an S3 bucket with public read access. Then, direct the algorithms you call to write their output into that S3 connector (or use s3utilities/UploadFiletoS3/ to move it there). Once it is in the S3 bucket, you can access the file via its publicly-readable URL.
Alternately, you could implement a small piece of backend code in another language and use it to retrieve the file for you.