How to create function of the eventing Service using the REST API?


Can I make a function with REST API instead of UI?

I referred to this site.

If you look here,

create a single Function

It says you can create a function, but I don’t know how.

I used this command.

curl -X POST http://Administrator:password@localhost:8096/api/v1/functions/testFunction3

result here.

I think we need to put some parameters on it, so I’d appreciate it if you could let me know if there’s a site for reference.

Or tell me if it’s impossible unless it’s in the UI.

Thank you.

Hi @leech

First my apologies the documentation for 6.5 and above does indeed need some improvements, especially for the REST API command you tried used. You need to provide POST data or a POST data file (this is not documented). Based on your issue, I have already updated the documents extensively with more detailed examples for versions 6.5, 6.6, and 7.0 (the later is in beta) you should see the updates in a few days.

I recommend reading my full response, but feel free to skip to the bottom where I show the solution to your issue.

I’m the Eventing PM, what you want to do is indeed possible, however it may not be supported (as in opening up a formal support ticket). The formal policy is to create your Eventing functions initially in the UI and only modify select settings via the REST API. I do want to point out right at the top of the Eventing REST API | Couchbase Docs page it does say explicitly.

Note: The Eventing Functions REST API endpoints on this page are supported, as long as the content of the handler body is not created or modified externally (as the internal format of the body is not yet standardized).

Okay with that said in the upcoming 6.6.X and 7.X releases this will change as we are defining formal schemas for Eventing that we will support. Refer to eventing/parser at master · couchbase/eventing · GitHub and look at the four (4) *_schema.json files. These files provide a formal definition to customers such that they could build Eventing functions via a CI/CD pipeline and fully automate the process. So eventually what you want to do will be formally supported.

Now let’s talk about what you tried to do, I’ll walk you through it first in the UI make an Eventing function example, I’ll call mine “testFunction3” just like you did, it is pretty simple (just logs a key and a random number) as follows:

function OnUpdate(doc, meta) {
    log("key",, 'random', Math.random());

Next “Export” the function from the UI it will create a “testFunction3.json” in your download area.

Note, I am using an internal 7.X server version (as such I might have a few extra properties than you will see) but if you pretty print the JSON payload of your exported function output you will get something like:

    "appcode":"function OnUpdate(doc, meta) {\n    log(\"key\",, 'random', Math.random());\n}",

Alternatively we could use the following REST API call to export the function (outside the UI)

curl -XGET http://Administrator:password@localhost:8096/api/v1/functions/testFunction3 -o testFunction3.json

Or even use the eventing CLI (also outside the UI)

couchbase-cli eventing-function-setup -c localhost -u Administrator -p password --export --name testFunction3 --file testFunction3.json

Now obviously I could by hand change things say update the “worker_count” from 3 to 6. Simple stuff like this won’t get you in trouble (but we don’t provide support or help customers write scripts or programs to do this). I am giving a walk through for 6.5.0 or above - if you have earlier versions you might need to use some of the deprecated functions.

The preferred methodology is to do the following via the REST API:

  • Test if the Eventing function is deployed (or paused)
  • Undeploy (or pause) the Eventing function (if deployed) via the REST API.
  • Update the Eventing function via the REST API.
  • Redeploy (or resume) the Eventing function (if needed).

Let’s do this and change the worker count from 3 to 6

Test if the Eventing function is deployed (or paused)

We first query the status (but this is across all functions - I actually have two right now), the composite status could be paused, deployed, undeployed, pausing, or deploying. You want either undeployed or paused (as in stable and not processing mutations).

curl -s http://Administrator:password@localhost:8096/api/v1/status | egrep "name"\|"composite_status"

output from the above command if undeployed will look like the following:

   "composite_status": "undeployed",
   "name": "txn_test",
   "composite_status": "deployed",
   "name": "testFunction3",

Undeploy (or pause) the Eventing function (if deployed) via the REST API.

Below I could substitute “undeploy” for “pause” the difference is to pick up where I left off or do a cold start when I re-deploy the Evening function.

curl -XPOST http://Administrator:password@localhost:8096/api/v1/functions/testFunction3/undeploy

It is up to you to test the “composite_status” as above as undeployment might take about 20 seconds.

Update the Eventing function via the REST API.

Now lets update our Eventing function via the rest API (not we are not modifying a file I will show this later). Note we are in an undeployed state we have to also supply “deployment_status”:false,“processing_status”:false (and in a paused state we also “deployment_status”:true,“processing_status”:false) in addition to the parameter we need to change “worker_count”: 6

curl -s -XPOST -d '{"deployment_status":false,"processing_status":false,"worker_count":6}' http://Administrator:password@localhost:8096/api/v1/functions/testFunction3/settings

The UI may not update until you hit a browser refresh if you desire to see the “change”.

Redeploy (or resume) the Eventing function (if needed).

curl -XPOST http://Administrator:password@localhost:8096/api/v1/functions/testFunction3/deploy

That’s it you’re done you updated your functions settings via the REST API in a CI/CD pipeline.

Now back to your question:

It says you can create a function, but I don’t know how.

Remember we exported the Function back when it had a worker_count of 3 (the default) lets manually edit the file and change it to 9. When you edit it it is NOT pretty printed (and this is prone to errors so be careful)

Undeploy the function (I assume it’s running so I not checking the status here)

curl -XPOST http://Administrator:password@localhost:8096/api/v1/functions/testFunction3/undeploy

I wait about 20 seconds (I don’t want to bother checking status once again) and I delete the function

curl -XDELETE http://Administrator:password@localhost:8096/api/v1/functions/testFunction3

Now I create the function from our exported and modified file (the required post data or the post datafile is missing from the documentation)

curl -XPOST -d @./testFunction3.json http://Administrator:password@localhost:8096/api/v1/functions/testFunction3

I hope this is enough to get you over the your hurdle and start using the Eventing REST API, shortly the documents will be updated to prevent others from hitting the same wall you experienced.


Jon Strabala

FYI, I have attached a ZIP file with a PDF of the pending documentation update which you should find helpful.
Eventing REST API - Couchbase Docs (Staging).zip (845.9 KB)

1 Like

HI @jon.strabala .

Thank you for your kind help.

After making the sample function, I can extract this function and make another function.

I didn’t know. Thank you.

Have a good day.