Skip to main content

Calling the REST API

Use the Modelbit REST API to connect predictions from deployed models to websites, application servers, and mobile apps.

Request Format

Calling a deployment involves sending POST request with a list of one or more sets of arguments for the deployment's main function.

For example, suppose the deployment's main function is example_deployment:

def example_deployment(foo: int, baz: int):
return foo + baz

A valid POST data for this deployment to add the numbers 10 and 11:

{
"data": [[1, 10, 11]]
}

Above, the number 1 is the ID of the input, and will be returned with the output of the method. The ID can be any string or number that's convenient for your use case.

To send more than one request in a batch, add more inputs to the data list:

{
"data": [
[1, 10, 11],
[2, 20, 21],
[3, 30, 31]
]
}

Response Format

The REST response for deployments is similar to the request format. The response is a list of the outputs from the deployment's main function, in the same order as the request's data batch, along with the IDs that were passed in.

The response from example_deployment called with the batch of 3 inputs above looks like this:

{
"data": [
[1, 21],
[2, 41],
[3, 61]
]
}

If there is an error during execution, it will be returned in an error field in the response.

Python Example

Calling example_deployment with the requests library:

import json, requests

requests.post("https://<your-workspace-url>/v1/example_deployment/latest",
headers={"Content-Type":"application/json"},
data=json.dumps({"data":[[1,10,11],[2,20,21],[3,30,31]]})).json()

Curl Example

Calling example_deployment with curl POST request:

curl -s -XPOST "https://<your-workspace-url>/v1/example_deployment/latest" \
-d '{"data":[[1,10,11],[2,20,21],[3,30,31]]}'

Using API Keys

API Keys are supplied with an Authorization header.

With curl, use the -H flag to set a header:

curl -s -XPOST "https://<your-workspace-url>/v1/example_deployment/latest" \
-H "Authorization: YOUR_API_KEY" \
-d '{"data":[[1,10,11],[2,20,21],[3,30,31]]}'

Or in Python, add to the headers= argument:

import json, requests

requests.post("https://<your-workspace-url>/v1/example_deployment/latest",
headers={
"Content-Type":"application/json",
"Authorization": "YOUR_API_KEY"
},
data=json.dumps({"data":[[1,10,11],[2,20,21],[3,30,31]]})).json()