> For the complete documentation index, see [llms.txt](https://codemash.gitbook.io/codemash/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://codemash.gitbook.io/codemash/cloud/code/marketplace/function.md).

# Add and call a function

Add a function of a marketplace connection, edit it, make a test call, disable or delete it.

**Where:** Code → Marketplace → a connection → **Add …** / **Edit …** · `/projects/<project>/code/marketplace/<integrationId>/functions/<functionKey>` (add) · `…/functions/<functionId>/edit` (edit)

A marketplace function is one function of a connection, saved under a name of your choice, with an id that starts with `func_`. You can add the same function several times with different names and bindings, then call each one by its id.

{% hint style="warning" %}
Two things do not work yet. **Add** in Cloud fails with *MarketplaceFunctionDto.RequestTemplate is required* (Cloud does not send the request template the server asks for), so add functions from code. And every call fails: **Run test call** and the Invoke Marketplace Function endpoint answer *A temporary internal error occurred* (`CM-ERRORS-INFRA-NORBIX-001`; the server log says *ExecutionContext not set*). Editing, disabling and deleting an added function work.
{% endhint %}

## Before you start

* An enabled connection — see [Enable and configure a connection](/codemash/cloud/code/marketplace/connection.md). Here: **Norbix Database** as `Bookshop database` (`int_4dotEqXT1UolAf5w6FqcMT`).
* For Norbix Database functions, a collection with records — here `books` (see [Database](/codemash/cloud/database.md)).
* You need `marketplace:create` to add, `marketplace:update` to edit, enable or disable, `marketplace:delete` to delete, and `marketplace:execute` to call a function.

## Steps

1. On the connection, press **Add …** next to a function, here **Insert record** (`db.insert`). The form **Add Insert record** opens.

   | Field              | Required | What it does                                                                                                                                                                                                                                                                                                   | Example                                                        |
   | ------------------ | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------- |
   | Display Name       | yes      | The name of this copy of the function; starts as the function's own name.                                                                                                                                                                                                                                      | `Add a book`                                                   |
   | Description        | no       | A note for your team.                                                                                                                                                                                                                                                                                          | `Adds one book to the books collection.`                       |
   | Payload parameters | —        | Read only: what the caller sends when calling the function.                                                                                                                                                                                                                                                    | `collection` (string, required), `document` (object, required) |
   | Bindings           | —        | Where each token of the request gets its value: **Default (connection / listing)**, **Fixed value** (for example `#alerts`) or **Dynamic binding** (for example `@Model.User.UserId`). Norbix Database functions have none: *This function has no bindable tokens — everything comes from the caller payload.* | —                                                              |

   <figure><img src="https://712692422-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy372bqsaex2cqS3ikhL%2Fuploads%2Fgit-blob-b87e5eff265980129a60c23f179a04ebdbc3210e%2Ffunction-1.png?alt=media" alt="Add Insert record with Display Name Add a book, the description, Payload parameters collection and document, and Bindings: This function has no bindable tokens"><figcaption><p>The add form.</p></figcaption></figure>
2. Press **Add**. Today Cloud goes back to the connection with the red box *MarketplaceFunctionDto.RequestTemplate is required*; nothing is added. Add the function from code ([Do it from code](#do-it-from-code)).

   <figure><img src="https://712692422-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy372bqsaex2cqS3ikhL%2Fuploads%2Fgit-blob-22e53735246424385773d9e3c9f33fe2e7dac27a%2Ffunction-2.png?alt=media" alt="The Norbix Database connection with the error MarketplaceFunctionDto.RequestTemplate is required; Added functions still shows only Find books"><figcaption><p>Add fails in Cloud.</p></figcaption></figure>
3. A function added from code is listed under **Added functions** with its name, **Enabled**, its function key and id. Press **Edit …**, here on `Find books` (`func_154strpl8cl6Kk4eL7k5Rj`). The editor shows the id, the **Enabled** badge, **Display Name**, **Description**, the payload parameters, the bindings, **Save**, and the panel **Make a test call** with the address the call goes to: `POST /code/marketplace/functions/func_154strpl8cl6Kk4eL7k5Rj/invoke`.

   <figure><img src="https://712692422-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy372bqsaex2cqS3ikhL%2Fuploads%2Fgit-blob-4c861496c07ecff36214793a9d0b0e2f0801ac44%2Ffunction-3.png?alt=media" alt="Editing Find books: func_154strpl8cl6Kk4eL7k5Rj, Enabled, Display Name, Description, payload parameters collection, filter and pageSize, Save, and Make a test call"><figcaption><p>The function editor.</p></figcaption></figure>
4. In **Make a test call**, type the payload your code would send and press **Run test call**:

   ```json
   {
     "collection": "books",
     "pageSize": 2
   }
   ```

   Today the answer under the button is the internal error.

   <figure><img src="https://712692422-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy372bqsaex2cqS3ikhL%2Fuploads%2Fgit-blob-fc58b8a37a95773ed6e275df49a8033071a5d6bb%2Ffunction-4.png?alt=media" alt="Run test call answered with isSuccess false and A temporary internal error occurred, CM-ERRORS-INFRA-NORBIX-001, ExecutionContext not set"><figcaption><p>The test call fails.</p></figcaption></figure>

## Result

An added function is listed under **Added functions** of its connection, and the function on **Available functions** gets the **Added** badge. Saving the editor changes the name and description. Once calling works, your code, database triggers and scheduled tasks call the function by its `func_…` id with the payload shown above.

## Change or remove it later

The **Actions** menu of the editor offers **Disable** (or **Enable**), **Delete** and **See marketplace documentation** (an old address that does not open). The row menu (⋮) on the connection page offers **Enable** / **Disable** too.

<figure><img src="https://712692422-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy372bqsaex2cqS3ikhL%2Fuploads%2Fgit-blob-70c8c60c377c82635ebad05c09b90de3613b0cc0%2Ffunction-5.png?alt=media" alt="The Actions menu of Find books: Disable, Delete, See marketplace documentation"><figcaption><p>Actions on a function.</p></figcaption></figure>

* **Disable** — the function stays but cannot be called.
* **Delete** — removes this copy of the function; only possible from the editor.
* If the connection is disabled, the editor shows *Connection is not enabled* and nothing can be changed.

## Do it from code

```ts
const integrationViewId = 'int_1Zz9o3jI1LJyWcRByuLduz'; // Shop web reader (Norbix Web)
await norbix.hub.code.saveMarketplaceFunction({
  integrationViewId,
  function: { integrationViewId, functionKey: 'web.readUrl', displayName: 'Read a page', isEnabled: true, requestTemplate: '{}' },
});
const { list } = await norbix.hub.code.getMarketplaceFunctions({ integrationViewId });
// [ 'func_60nvkEMafs7je0IIpAiJRf Read a page web.readUrl enabled=true' ]

// fails today with CM-ERRORS-INFRA-NORBIX-001
await norbix.hub.code.invokeMarketplaceFunction({
  functionViewId: 'func_154strpl8cl6Kk4eL7k5Rj',
  payload: { collection: 'books', pageSize: 2 },
});
```

```sh
norbix hub code marketplace function invoke --functionViewId func_154strpl8cl6Kk4eL7k5Rj --payload:json '{"collection":"books","pageSize":2}'
```

The save answers success without the new id; read it with Get Marketplace Functions. The CLI call answers *A temporary internal error occurred* today. API: [Save Marketplace Function](/codemash/api-reference/code/marketplace/save-marketplace-function.md) · [Get Marketplace Functions](/codemash/api-reference/code/marketplace/get-marketplace-functions.md) · [Invoke Marketplace Function](/codemash/api-reference/code/marketplace/invoke-marketplace-function.md) · [Enable](/codemash/api-reference/code/marketplace/enable-marketplace-function.md) / [Disable](/codemash/api-reference/code/marketplace/disable-marketplace-function.md) / [Delete Marketplace Function](/codemash/api-reference/code/marketplace/delete-marketplace-function.md) · [Get Marketplace Function Tokens](/codemash/api-reference/code/marketplace/get-marketplace-function-tokens.md)
