Skip to content

How to build an endpoint on Blueprint

Use endpoints in your Blueprint projects to build APIs backed by foundation models. You can build and test endpoints in the Blueprint IDE, then invoke them from anywhere.

In this tutorial, we're going to build an endpoint that uses Whisper to transcribe audio files. You'll need an API key to complete this tutorial.

Building an endpoint

Building an endpoint starts with defining a route using the @route() decorator. Use this decorator to assign a URL to a function, like so:

@route(path="/invoke-whisper")
def run_whisper(request):
    return request

You'll notice the invoke-whisper route appear on the left sidebar and in the playground (more on that in a moment).

From there, we need to implement the actual function. Thanks to the baseten.models package, we can use Whisper right out of the box for our endpoint. The whole function is just four lines of code:

from baseten import route
from baseten.models import Whisper

@route(path="/invoke-whisper")
def run_whisper(request):
    input_url = request["body"]["url"]
    model = Whisper()
    output = model(input_url)
    return {
        "transcript": output["text"]
    }

After accessing the URL passed in the request body, our function calls Whisper and strips out the relevant text from the dictionary created by the model. The return value must be JSON-serializable, so we return a dictionary.

Invoking our new endpoint

We built this thing, let's take it for a test drive!

Playground invocation

The fastest way to try out your endpoint is in the playground, which shows up at the bottom of your screen. Use the playground to quickly iterate as you develop your endpoints.

This endpoint will take a POST request, which you can select from the dropdown menu in the playground. To invoke this endpoint, use the following JSON:

{
  "headers": {"Content-Type": "application/json"},
  "query": {},
  "body": {"url": "https://cdn.baseten.co/docs/production/Gettysburg.mp3"}
}

The result of the invocation will show in the right side of the window. It should look like this:

{
    "transcript":"Four score and seven years ago, our fathers brought forth upon this continent a new nation conceived in liberty and dedicated to the proposition that all men are created equal."
}

API invocation

Once you're satisfied with your endpoint, you can use it in any application. To invoke the endpoint outside of the Blueprint IDE, you'll need an API key.

Info

Find your PROJECT_ID for API invocation in the URL of your Blueprint project.

e.g. in https://app.baseten.co/blueprint/projects/abcd0123, abcd0123 is your PROJECT_ID.

You can invoke the endpoint any way you like: Python, cURL, JavaScript, carrier pigeon. Here's an example in Python (because I don't know how to code in pigeon):

curl -X POST https://app.baseten.co/routes/PROJECT_ID/invoke-whisper \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Api-Key YOUR_API_KEY' \
  -d '{"url": "https://cdn.baseten.co/docs/production/Gettysburg.mp3"}'

Making the endpoint public

Let's say you want the whole world to be able to invoke your endpoint.

Be careful! Unsecured endpoints could receive substantial traffic or be used for malicious purposes. So be thoughtful about which endpoints you leave unsecured.

To make an endpoint public, pass public=True to the route decorator, like so:

@route(path="/invoke-whisper", public=True)
def run_whisper(request):
    pass

Then, you can invoke the endpoint in the same way, but without passing the API key.