Use the copilot to write function code
Not sure where to start with TypeScript or the C1 API? The built-in AI code assistant can generate a working function from a plain-language description of what you want it to do — no TypeScript expertise required. Since functions start as drafts, you can try out the generated code, run it, and iterate safely before publishing. To get started, click Create with AI when creating a new function, or click Edit with AI in the code editor of an existing function draft. Describe what you want your function to do, and the AI assistant will generate code to get you started. You can then edit the code as needed, run it with test inputs, and publish when you’re ready.Step 1: Set up a new function
1
Navigate to Workflows > Functions.
2
Click New function.
3
Enter a name and optional description, then click Create.
4
You’re taken to the detail page with a built-in code editor (Monaco, TypeScript mode). The editor includes two files:
main.ts for your function code and main.test.ts for tests.Step 2: Write function code
Every function must export amain function that accepts input and returns a result. Both input and output are JSON objects.
Basic structure
Use input parameters
Accept input to make your function interactive:Access C1 data
Use the pre-authenticatedsdk object to query data from your C1 tenant.
Functions authenticate to the C1 API as a service principal. Before publishing, link a service principal whose roles match what your function needs to do. See Step 5: Link a service principal for API access below.
List users
Get a specific user
Search users
Use secrets and configuration
Store API keys and configuration values securely in your function’s config.Set secrets in the UI
1
Navigate to your function and click Edit config (in the more menu).
2
In the Secrets section, add key/value pairs.
3
Click Save.
Access secrets in code
Call external APIs
Step 3: Handle errors
Functions should handle errors gracefully and return useful information for debugging.Try-catch pattern
Input validation
Step 4: Test your function
There are two ways to test your function: manual invocation and automated tests.Manual invocation
1
Click Run draft to invoke your function with test JSON input.
2
Provide test input in the JSON editor, for example:
{}3
View the output and logs in the invocation details drawer.
Automated tests
New functions include amain.test.ts file with a starter template. Write tests using the @c1/test framework:
main function as handler, so each test can call it with different inputs.
1
Click Test draft (or Run tests when viewing published code) to execute your tests.
2
View individual test results in the dialog. Each
test() call becomes its own result with a pass/fail status.3
Click on a test result to expand assertion details and logs.
Step 5: Link a service principal for API access
Functions authenticate to the C1 API as a linked service principal. Its role bindings determine what your function can do — read-only, write, or anything in between.1
If you don’t already have a suitable service principal, create one. Navigate to Directory > Service principals > New, give it a descriptive name, and assign roles based on what your function needs (for example, Read-Only Administrator for a function that only reads, or Super Administrator for a function that creates or updates resources).
2
Open your function and click … (the more actions menu) > Link service principal.
3
Pick the service principal from the list and click Save.
Existing functions. Functions that existed before this change have been migrated automatically and linked to a service principal with read-only access. No action is required to keep them running. To grant write access, link them to a different service principal using the steps above.
Step 6: Publish your function
1
Click Save draft to commit your changes.
2
Click Publish to make your function available for use across C1.