Create custom API connections using JavaScript code
Create initial Code API Connection
Set Up Allowed Domains
api.example.com
)*
) are supported for subdomainsasappUtilities.callAPI(url, apiRequest)
Manage Environment Variables
asappUtilities.getEnvVariable("VARIABLE_NAME")
Add Authentication Methods
Define Request and Response Schemas
schemas/request.json
) - Define the request schema that your function will receiveschemas/response.json
) - Define the response schema that your function will returnImplement Your Function
handleRequest(request)
function in src/index.js
that takes the request object and returns the response object.See the Implementing the Handle Function section below for detailed information on how to implement your function.Test Your Code
request.json
schemaSave and Deploy
schemas/request.json
) defines the structure of data that your function will receive. This JSON schema is both the request schema that your function will receive and the parameters shown to GenerativeAgent.
Add the variables you want as input. Ensure the name and description of each variable is easy for GenerativeAgent to understand.
schemas/response.json
) defines the structure of data that your function must return. This is the response that is shown to GenerativeAgent.
Add the variables you want as output. Ensure the name and description of each variable is easy for GenerativeAgent to understand.
handleRequest(request)
function in src/index.js
is the core of your API connection. This function takes the request object and returns the response object.
When first created, your function looks like this:
NOT_IMPLEMENTED
error with your own implementation for the API connection.
request
parameter contains the data defined in your request schema.
several error classes in the asappUtilities
library. Always use one of these error classes for error handling to ensure proper error propagation to ASAPP:
callAPI
function. This function uses fetch under the hood and follows fetch’s interface.
This is the only way to make external HTTP API calls from your code.
asappUtilities
error:
asappUtilities.APIConnectionError
asappUtilities.ClientAuthenticationError
context
object.
Available Context Data
context
object. Access it using dot notation.asappUtilities.callAPI()
.
There may be times you want to pull out data from the authentication method, such grabbing claims from a JWT token. You can access the authentication methods you’ve configured in your code using the asappUtilities.getAuthMethod()
function.
asappUtilities.callAPI()
, specify which authentication method to use by name for each environment:
authMethods
object maps environment names to the exact names of your configured authentication methods. ASAPP automatically applies the appropriate authentication (e.g. tokens, api keys, etc.) to the headers based on the method you’ve configured for each environment.
Important: The authentication method names in your code must exactly match the names you gave them when creating the authentication methods in the Settings → Authentication Methods section.
console.log
statements will be displayed in the Run panel.
To specify the data that will be passed to your function when running, in the right hand run panel, you need to specify:
request.json
schema.require()
or import
statements.
If you require additional third-party libraries or tools for your integration, reach out to your ASAPP account team to discuss your specific needs.