REST API (other languages)

Requires BAML version >=0.55

This feature is a preview feature and may change. Please provide feedback either in Discord or on GitHub so that we can stabilize the feature and keep you updated!

BAML allows you to expose your BAML functions as RESTful APIs:

We integrate with OpenAPI (universal API definitions), so you can get typesafe client libraries for free!

1

Install BAML VSCode Extension

https://marketplace.visualstudio.com/items?itemName=boundary.baml-extension

  • syntax highlighting
  • testing playground
  • prompt previews
2

Install NPX + OpenAPI

$brew install npm openapi-generator
># 'npm' will install npx
># 'openapi-generator' will install both Java and openapi-generator-cli
3

Add BAML to your existing project

This will give you some starter BAML code in a baml_src directory.

$npx @boundaryml/baml init \
> --client-type rest/openapi --openapi-client-type csharp
4

Start the BAML development server

$npx @boundaryml/baml dev --preview

This will do four things:

  • serve your BAML functions over a RESTful interface on localhost:2024
  • generate an OpenAPI schema in baml_client/openapi.yaml
  • run openapi-generator -g $OPENAPI_CLIENT_TYPE in baml_client directory to generate an OpenAPI client for you to use
  • re-run the above steps whenever you modify any .baml files

BAML-over-REST is currently a preview feature. Please provide feedback either in Discord or on GitHub so that we can stabilize the feature and keep you updated!

5

Use a BAML function in any language!

openapi-generator will generate a README with instructions for installing and using your client; we’ve included snippets for some of the most popular languages below. Check out baml-examples for example projects with instructions for running them.

We’ve tested the below listed OpenAPI clients, but not all of them. If you run into issues with any of the OpenAPI clients, please let us know, either in Discord or by commenting on GitHub so that we can either help you out or fix it!

Run this with go run main.go:

main.go
1package main
2
3import (
4 "context"
5 "fmt"
6 "log"
7 baml "my-golang-app/baml_client"
8)
9
10func main() {
11 cfg := baml.NewConfiguration()
12 b := baml.NewAPIClient(cfg).DefaultAPI
13 extractResumeRequest := baml.ExtractResumeRequest{
14 Resume: "Ada Lovelace (@gmail.com) was an English mathematician and writer",
15 }
16 resp, r, err := b.ExtractResume(context.Background()).ExtractResumeRequest(extractResumeRequest).Execute()
17 if err != nil {
18 fmt.Printf("Error when calling b.ExtractResume: %v\n", err)
19 fmt.Printf("Full HTTP response: %v\n", r)
20 return
21 }
22 log.Printf("Response from server: %v\n", resp)
23}