Create function
Create a new serverless function in draft status. The function will be saved but not deployed to the runtime platform.
After creating a function:
- Review and test the code locally
- Deploy using POST /functions//deploy
- Invoke using POST /functions//invoke
Choose function_type based on your deployment requirements:
cloudflare_worker: Fast global edge deployment with standard JavaScriptsupabase_function: Deno runtime with built-in Supabase client
The function slug will be auto-generated from the name if not provided (lowercase with hyphens).
curl --request POST \
--url https://api.kapso.ai/platform/v1/functions \
--header 'Content-Type: application/json' \
--header 'X-API-Key: <api-key>' \
--data @- <<EOF
{
"function": {
"name": "Calculate Shipping Cost",
"code": "export default async function(request) {\n const { weight, distance, service } = await request.json();\n const baseCost = weight * 0.5;\n const distanceCost = distance * 0.1;\n const serviceFee = service === 'express' ? 10 : 0;\n return new Response(JSON.stringify({\n cost: baseCost + distanceCost + serviceFee\n }));\n}\n",
"function_type": "cloudflare_worker",
"slug": "calculate-shipping-cost",
"description": "Calculates shipping cost based on weight, distance, and service level",
"public_endpoint": false,
"runtime_config": {
"timeout": 30,
"memory": 128
}
}
}
EOFimport requests
url = "https://api.kapso.ai/platform/v1/functions"
payload = { "function": {
"name": "Calculate Shipping Cost",
"code": "export default async function(request) {
const { weight, distance, service } = await request.json();
const baseCost = weight * 0.5;
const distanceCost = distance * 0.1;
const serviceFee = service === 'express' ? 10 : 0;
return new Response(JSON.stringify({
cost: baseCost + distanceCost + serviceFee
}));
}
",
"function_type": "cloudflare_worker",
"slug": "calculate-shipping-cost",
"description": "Calculates shipping cost based on weight, distance, and service level",
"public_endpoint": False,
"runtime_config": {
"timeout": 30,
"memory": 128
}
} }
headers = {
"X-API-Key": "<api-key>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'X-API-Key': '<api-key>', 'Content-Type': 'application/json'},
body: JSON.stringify({
function: {
name: 'Calculate Shipping Cost',
code: 'export default async function(request) {\n const { weight, distance, service } = await request.json();\n const baseCost = weight * 0.5;\n const distanceCost = distance * 0.1;\n const serviceFee = service === \'express\' ? 10 : 0;\n return new Response(JSON.stringify({\n cost: baseCost + distanceCost + serviceFee\n }));\n}\n',
function_type: 'cloudflare_worker',
slug: 'calculate-shipping-cost',
description: 'Calculates shipping cost based on weight, distance, and service level',
public_endpoint: false,
runtime_config: {timeout: 30, memory: 128}
}
})
};
fetch('https://api.kapso.ai/platform/v1/functions', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.kapso.ai/platform/v1/functions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'function' => [
'name' => 'Calculate Shipping Cost',
'code' => 'export default async function(request) {
const { weight, distance, service } = await request.json();
const baseCost = weight * 0.5;
const distanceCost = distance * 0.1;
const serviceFee = service === \'express\' ? 10 : 0;
return new Response(JSON.stringify({
cost: baseCost + distanceCost + serviceFee
}));
}
',
'function_type' => 'cloudflare_worker',
'slug' => 'calculate-shipping-cost',
'description' => 'Calculates shipping cost based on weight, distance, and service level',
'public_endpoint' => false,
'runtime_config' => [
'timeout' => 30,
'memory' => 128
]
]
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"X-API-Key: <api-key>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.kapso.ai/platform/v1/functions"
payload := strings.NewReader("{\n \"function\": {\n \"name\": \"Calculate Shipping Cost\",\n \"code\": \"export default async function(request) {\\n const { weight, distance, service } = await request.json();\\n const baseCost = weight * 0.5;\\n const distanceCost = distance * 0.1;\\n const serviceFee = service === 'express' ? 10 : 0;\\n return new Response(JSON.stringify({\\n cost: baseCost + distanceCost + serviceFee\\n }));\\n}\\n\",\n \"function_type\": \"cloudflare_worker\",\n \"slug\": \"calculate-shipping-cost\",\n \"description\": \"Calculates shipping cost based on weight, distance, and service level\",\n \"public_endpoint\": false,\n \"runtime_config\": {\n \"timeout\": 30,\n \"memory\": 128\n }\n }\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("X-API-Key", "<api-key>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.kapso.ai/platform/v1/functions")
.header("X-API-Key", "<api-key>")
.header("Content-Type", "application/json")
.body("{\n \"function\": {\n \"name\": \"Calculate Shipping Cost\",\n \"code\": \"export default async function(request) {\\n const { weight, distance, service } = await request.json();\\n const baseCost = weight * 0.5;\\n const distanceCost = distance * 0.1;\\n const serviceFee = service === 'express' ? 10 : 0;\\n return new Response(JSON.stringify({\\n cost: baseCost + distanceCost + serviceFee\\n }));\\n}\\n\",\n \"function_type\": \"cloudflare_worker\",\n \"slug\": \"calculate-shipping-cost\",\n \"description\": \"Calculates shipping cost based on weight, distance, and service level\",\n \"public_endpoint\": false,\n \"runtime_config\": {\n \"timeout\": 30,\n \"memory\": 128\n }\n }\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.kapso.ai/platform/v1/functions")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["X-API-Key"] = '<api-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"function\": {\n \"name\": \"Calculate Shipping Cost\",\n \"code\": \"export default async function(request) {\\n const { weight, distance, service } = await request.json();\\n const baseCost = weight * 0.5;\\n const distanceCost = distance * 0.1;\\n const serviceFee = service === 'express' ? 10 : 0;\\n return new Response(JSON.stringify({\\n cost: baseCost + distanceCost + serviceFee\\n }));\\n}\\n\",\n \"function_type\": \"cloudflare_worker\",\n \"slug\": \"calculate-shipping-cost\",\n \"description\": \"Calculates shipping cost based on weight, distance, and service level\",\n \"public_endpoint\": false,\n \"runtime_config\": {\n \"timeout\": 30,\n \"memory\": 128\n }\n }\n}"
response = http.request(request)
puts response.read_body{
"data": {
"id": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
"name": "<string>",
"slug": "calculate-shipping-cost",
"status": "draft",
"function_type": "cloudflare_worker",
"created_at": "2023-11-07T05:31:56Z",
"updated_at": "2023-11-07T05:31:56Z",
"lock_version": 0,
"description": "<string>",
"code": "<string>",
"version": 123,
"last_deployed_at": "2023-11-07T05:31:56Z",
"invoke_response_mode": "passthrough",
"public_endpoint": false,
"runtime_config": {},
"endpoint_url": "https://api.kapso.ai/platform/v1/functions/{function_id}/invoke",
"project_id": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
"created_by_id": "3c90c3cc-0d44-4b50-8888-8dd25736052a"
}
}{
"error": "<string>"
}{
"error": "<string>"
}Authorizations
Body
Request to create a new serverless function. The function will be saved in draft status. Use POST /functions/{id}/deploy to deploy it to the runtime platform.
Show child attributes
Show child attributes
Response
Function created successfully
Single function response
Serverless function configuration. Functions are custom JavaScript code that runs on-demand in response to API invocations. Deploy functions to either Cloudflare Workers (global edge network) or Supabase Edge Functions (Deno runtime).
Show child attributes
Show child attributes
Was this page helpful?
curl --request POST \
--url https://api.kapso.ai/platform/v1/functions \
--header 'Content-Type: application/json' \
--header 'X-API-Key: <api-key>' \
--data @- <<EOF
{
"function": {
"name": "Calculate Shipping Cost",
"code": "export default async function(request) {\n const { weight, distance, service } = await request.json();\n const baseCost = weight * 0.5;\n const distanceCost = distance * 0.1;\n const serviceFee = service === 'express' ? 10 : 0;\n return new Response(JSON.stringify({\n cost: baseCost + distanceCost + serviceFee\n }));\n}\n",
"function_type": "cloudflare_worker",
"slug": "calculate-shipping-cost",
"description": "Calculates shipping cost based on weight, distance, and service level",
"public_endpoint": false,
"runtime_config": {
"timeout": 30,
"memory": 128
}
}
}
EOFimport requests
url = "https://api.kapso.ai/platform/v1/functions"
payload = { "function": {
"name": "Calculate Shipping Cost",
"code": "export default async function(request) {
const { weight, distance, service } = await request.json();
const baseCost = weight * 0.5;
const distanceCost = distance * 0.1;
const serviceFee = service === 'express' ? 10 : 0;
return new Response(JSON.stringify({
cost: baseCost + distanceCost + serviceFee
}));
}
",
"function_type": "cloudflare_worker",
"slug": "calculate-shipping-cost",
"description": "Calculates shipping cost based on weight, distance, and service level",
"public_endpoint": False,
"runtime_config": {
"timeout": 30,
"memory": 128
}
} }
headers = {
"X-API-Key": "<api-key>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'X-API-Key': '<api-key>', 'Content-Type': 'application/json'},
body: JSON.stringify({
function: {
name: 'Calculate Shipping Cost',
code: 'export default async function(request) {\n const { weight, distance, service } = await request.json();\n const baseCost = weight * 0.5;\n const distanceCost = distance * 0.1;\n const serviceFee = service === \'express\' ? 10 : 0;\n return new Response(JSON.stringify({\n cost: baseCost + distanceCost + serviceFee\n }));\n}\n',
function_type: 'cloudflare_worker',
slug: 'calculate-shipping-cost',
description: 'Calculates shipping cost based on weight, distance, and service level',
public_endpoint: false,
runtime_config: {timeout: 30, memory: 128}
}
})
};
fetch('https://api.kapso.ai/platform/v1/functions', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.kapso.ai/platform/v1/functions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'function' => [
'name' => 'Calculate Shipping Cost',
'code' => 'export default async function(request) {
const { weight, distance, service } = await request.json();
const baseCost = weight * 0.5;
const distanceCost = distance * 0.1;
const serviceFee = service === \'express\' ? 10 : 0;
return new Response(JSON.stringify({
cost: baseCost + distanceCost + serviceFee
}));
}
',
'function_type' => 'cloudflare_worker',
'slug' => 'calculate-shipping-cost',
'description' => 'Calculates shipping cost based on weight, distance, and service level',
'public_endpoint' => false,
'runtime_config' => [
'timeout' => 30,
'memory' => 128
]
]
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"X-API-Key: <api-key>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.kapso.ai/platform/v1/functions"
payload := strings.NewReader("{\n \"function\": {\n \"name\": \"Calculate Shipping Cost\",\n \"code\": \"export default async function(request) {\\n const { weight, distance, service } = await request.json();\\n const baseCost = weight * 0.5;\\n const distanceCost = distance * 0.1;\\n const serviceFee = service === 'express' ? 10 : 0;\\n return new Response(JSON.stringify({\\n cost: baseCost + distanceCost + serviceFee\\n }));\\n}\\n\",\n \"function_type\": \"cloudflare_worker\",\n \"slug\": \"calculate-shipping-cost\",\n \"description\": \"Calculates shipping cost based on weight, distance, and service level\",\n \"public_endpoint\": false,\n \"runtime_config\": {\n \"timeout\": 30,\n \"memory\": 128\n }\n }\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("X-API-Key", "<api-key>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.kapso.ai/platform/v1/functions")
.header("X-API-Key", "<api-key>")
.header("Content-Type", "application/json")
.body("{\n \"function\": {\n \"name\": \"Calculate Shipping Cost\",\n \"code\": \"export default async function(request) {\\n const { weight, distance, service } = await request.json();\\n const baseCost = weight * 0.5;\\n const distanceCost = distance * 0.1;\\n const serviceFee = service === 'express' ? 10 : 0;\\n return new Response(JSON.stringify({\\n cost: baseCost + distanceCost + serviceFee\\n }));\\n}\\n\",\n \"function_type\": \"cloudflare_worker\",\n \"slug\": \"calculate-shipping-cost\",\n \"description\": \"Calculates shipping cost based on weight, distance, and service level\",\n \"public_endpoint\": false,\n \"runtime_config\": {\n \"timeout\": 30,\n \"memory\": 128\n }\n }\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.kapso.ai/platform/v1/functions")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["X-API-Key"] = '<api-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"function\": {\n \"name\": \"Calculate Shipping Cost\",\n \"code\": \"export default async function(request) {\\n const { weight, distance, service } = await request.json();\\n const baseCost = weight * 0.5;\\n const distanceCost = distance * 0.1;\\n const serviceFee = service === 'express' ? 10 : 0;\\n return new Response(JSON.stringify({\\n cost: baseCost + distanceCost + serviceFee\\n }));\\n}\\n\",\n \"function_type\": \"cloudflare_worker\",\n \"slug\": \"calculate-shipping-cost\",\n \"description\": \"Calculates shipping cost based on weight, distance, and service level\",\n \"public_endpoint\": false,\n \"runtime_config\": {\n \"timeout\": 30,\n \"memory\": 128\n }\n }\n}"
response = http.request(request)
puts response.read_body{
"data": {
"id": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
"name": "<string>",
"slug": "calculate-shipping-cost",
"status": "draft",
"function_type": "cloudflare_worker",
"created_at": "2023-11-07T05:31:56Z",
"updated_at": "2023-11-07T05:31:56Z",
"lock_version": 0,
"description": "<string>",
"code": "<string>",
"version": 123,
"last_deployed_at": "2023-11-07T05:31:56Z",
"invoke_response_mode": "passthrough",
"public_endpoint": false,
"runtime_config": {},
"endpoint_url": "https://api.kapso.ai/platform/v1/functions/{function_id}/invoke",
"project_id": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
"created_by_id": "3c90c3cc-0d44-4b50-8888-8dd25736052a"
}
}{
"error": "<string>"
}{
"error": "<string>"
}
