Scoped mode is in the beta phase of development. Functionality may change during active development.
Scoped mode lets you declare the resources and markings a function can access. Foundry uses this declaration to create a restricted execution token before it runs the function. A scoped function can only access data that both its scope and the caller's permissions allow.
A scope contains two parts:
max).Scoped mode does not grant the caller additional permissions. The caller must still have the access required to use the declared resources.
Use scoped mode for new functions where possible. A scope limits each function to the resources and markings it needs, and it is the preferred way to call actions or read datasets directly from a function.
| Option | When to use it |
|---|---|
| Scoped mode (recommended) | Your function calls actions, reads datasets directly, or needs explicit resource and marking boundaries. Scoped mode also suits functions that only use standard capabilities. |
| Standard function execution | Your function only uses standard function capabilities and you are not ready to declare a scope. |
| Extended function execution in Control Panel (to be deprecated) | Your function needs an extended token lifetime, or needs extended functionality but cannot use scoped mode because of its limitations. See Functions settings. |
In a TypeScript v2 or Python functions repository that supports scoped mode:
Open the Resource imports panel.
Select Scope settings, identified by the shield icon.

Turn on Enable scoped mode.

Import the resources the functions need and review their aliases and markings.
The repository stores imported resources, marking aliases, and default scope configuration in resources.json. Use the import interface to maintain these entries. Turning on Enable scoped mode sets "enableScopedMode": true in the repository's functions.json. Adding a scope declaration in code does not itself enable scoped mode; without the setting, scope declarations are ignored.
Publish a new function version after changing its scope, and update consuming applications to use that version.
You can move an existing repository to scoped mode without adding a scope declaration to every function first. When scoped mode is enabled, functions that do not declare a scope use the default resources defined by the defaultResources entry in resources.json, with no authorization constraint. If defaultResources is missing, publishing fails for those functions.
An explicitly declared empty scope, such as SecurityScope() in Python, declares no resources and no authorization, and does not fall back to the defaults.
In TypeScript v2, export a config object from the function's source file. In Python, pass a SecurityScope to the @function decorator.
For many functions, the repository defaults are sufficient, and they behave most like standard function execution. Explicit resources and authorizations exist to impose additional restrictions on a function; declare them only when you want to limit what the function can access.
The following examples explicitly select the repository's default resources. Because they omit authorization, they impose no explicit maximum authorization. Resource restrictions and the caller's permissions still apply.
Copied!1 2 3 4 5 6 7 8 9 10 11 12import type { FunctionConfig } from "@osdk/functions"; import { ScopeResources } from "@osdk/functions/experimental"; export const config: FunctionConfig = { scope: { resources: ScopeResources.defaultResources, }, }; export default function greet(name: string): string { return `Hello, ${name}`; }
Copied!1 2 3 4 5 6 7 8 9 10from functions.api import Resources, SecurityScope, function @function( scope=SecurityScope( resources=Resources.default_resources, ) ) def greet(name: str) -> str: return f"Hello, {name}"
When scoped mode is enabled, a function that declares no scope is equivalent to the examples above. See Migrate an existing repository with the default scope for more information:
Copied!1 2 3export default function greet(name: string): string { return `Hello, ${name}`; }
Copied!1 2 3 4 5 6from functions.api import function @function def greet(name: str) -> str: return f"Hello, {name}"
The default resources selector uses defaultResources from resources.json.
Adding imports can change the default scope used when functions are next published.
Declare an explicit scope when you want to restrict a function further than the repository defaults, limiting it to only the resources and markings it requires. A narrower scope makes the function more secure, because its token cannot access anything else in the repository. An explicit scope also does not change when you add imports to the repository.
Declare every resource the function uses. Token creation only checks the resources in the scope, so a function that accesses an undeclared resource fails at runtime with a permission error.
Reference resources and markings by their imported aliases. To find the alias of each imported resource and marking, as well as the repository's defaultResources, check resources.json.
The following example declares a scope for a function that needs orders, customers, a pricing dataset, and an update action. The function body only returns its input; replace it with your application logic.
Copied!1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18import type { FunctionConfig } from "@osdk/functions"; export const config: FunctionConfig = { scope: { authorization: { max: ["commercialData", "personalData"], }, resources: { objects: ["orders", "customers"], datasets: ["pricingData"], actions: ["updateOrder"], }, }, }; export default function processOrder(orderId: string): string { return orderId; }
Copied!1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17from functions.api import Authorization, Resources, SecurityScope, function @function( scope=SecurityScope( authorization=Authorization( max=["commercialData", "personalData"], ), resources=Resources( objects=["orders", "customers"], datasets=["pricingData"], actions=["updateOrder"], ), ) ) def process_order(order_id: str) -> str: return order_id
Replace the resource and marking aliases with aliases imported into your repository. In this example, the required inputs are protected by the commercialData and personalData markings.
In TypeScript v2, read is deprecated but still supported, and sets the function's maximum authorization. A scope cannot declare read together with max. In Python, Authorization(read=...) is not supported; use max instead.
Resource fields share the same names in TypeScript v2 and Python: objects, links, interfaces, actions, queries (imported functions), datasets, mediasets, streams, and models. When you declare links, also declare the object types they connect.
Every resource field accepts alias strings. TypeScript v2 also accepts OSDK references for objects, interfaces, queries, and actions. Python currently accepts OSDK references only for interfaces.
For example, the following declarations both select the Vehicle interface, first by its alias vehicle and then by OSDK reference:
Copied!1 2 3 4import { Vehicle } from "@ontology/sdk"; const byAlias = { interfaces: ["vehicle"] }; const byReference = { interfaces: [Vehicle] };
Copied!1 2 3 4 5from functions.api import Resources from ontology_sdk.ontology.interfaces import Vehicle by_alias = Resources(interfaces=["vehicle"]) by_reference = Resources(interfaces=[Vehicle])
The authorization bounds the markings the function's execution token can use. Declare it as a maximum (max): the markings the function can run with. Include the markings required by the resources and dependencies the function reads. If you omit it, the scope sets no explicit maximum, and the function is still limited by its declared resources and the caller's access.
A flat list, such as ["commercialData", "personalData"], combines the markings with AND, so the bound includes all listed markings.
A scoped function runs with the markings that the caller can access, limited by the function's maximum authorization, if declared. The execution token cannot access data with markings outside these, even if the caller can access them. When a scoped function calls another function or applies an action, the called function runs within the same bounds.
Live preview of a scoped function uses its declared resources and authorization and your own access, so preview shows how the published function behaves under its scope.
Resource access and marking authorization are separate requirements:
customers to the resource scope does not add its required markings. If the maximum authorization does not include every marking on a declared resource, token creation fails.personalData to the maximum authorization does not grant access to every resource protected by that marking.If a function calls another function or applies an action, it must be compatible with that dependency in both directions. Foundry checks this before it runs the function, and token creation fails if a check does not pass:
For example, the getPrice function reads from the pricingData dataset, which is marked as commercialData. If the calling function declares a maximum authorization, it must include commercialData, even if the function does not read from pricingData directly:
Copied!1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18import { Client } from "@osdk/client"; import { Double, type FunctionConfig } from "@osdk/functions"; import { getPrice } from "@ontology/sdk"; export const config: FunctionConfig = { scope: { authorization: { max: ["commercialData"], }, resources: { queries: ["getPrice"], }, }, }; export default async function quoteOrder(client: Client, orderId: string): Promise<Double> { return client(getPrice).executeFunction({ orderId }); }
Copied!1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16from functions.api import Authorization, Double, Resources, SecurityScope, function from ontology_sdk import FoundryClient @function( scope=SecurityScope( authorization=Authorization( max=["commercialData"], ), resources=Resources( queries=["getPrice"], ), ) ) def quote_order(order_id: str) -> Double: return FoundryClient().ontology.queries.get_price(order_id=order_id)
If the calling function declares a maximum authorization that excludes commercialData, token creation fails.
The upper bound applies to the same example. If getPrice declares a maximum authorization of only commercialData, a function that declares both commercialData and personalData cannot call getPrice. To resolve this, either remove personalData from the calling function's maximum authorization, or add personalData to the maximum authorization of getPrice.
Action types can also require markings. An action type with a configured authorization requires callers to run with exactly that authorization. A function that applies such an action must run with a matching authorization. Token creation fails if the execution does not satisfy the action's authorization constraints. Missing required markings produces an inputMinimumAuthorization violation; exceeding the permitted authorization produces an inputMaximumAuthorization violation.
Scoped mode supports direct reads from declared datasets and execution of declared actions. It also supports the other resource types listed in Resources.
The supported operations depend on the resource type. Declaring a dataset, stream, or media set currently provides read access; it does not enable arbitrary writes to that resource.
Scoped mode currently has these limitations:
StreamingFunctionsUnsupported error.A stateful function can retain data between executions. Restricting the token for one execution does not isolate data retained from an earlier execution.
Action execution still enforces the action's permissions and submission requirements.
A scoped function can fail at three stages: when you publish it, when Foundry creates its execution token, or after the token is created and the function starts running.
Foundry checks the scope declaration against resources.json when you publish a function:
| Cause | Fix |
|---|---|
The function declares no scope or selects the default resources, but resources.json does not declare defaultResources | Configure the repository's default resources, or declare an explicit scope. |
| A resource or marking alias is not found | Import the resource or marking into the repository, and use the alias exactly as it appears in resources.json. |
| An alias matches more than one resource or marking | Give the entries distinct aliases in resources.json, and update the declaration. |
| The scope declares an operation that scoped mode does not support, such as writing to a dataset | Remove the operation from the resource's imported operations in the Resource imports panel. To modify data, apply a declared action. |
| A resource is imported without the operation that scoped mode requires for it | Add the required operation to the resource's imported operations in the Resource imports panel. |
A Python scope uses Authorization(read=...) | Use Authorization(max=...) instead. |
A TypeScript v2 scope declares read together with max, or an unsupported authorization field | Declare only max. read is deprecated. |
Publishing does not check markings or project imports. Those checks run when Foundry creates the execution token; see Token creation fails.
Before it creates a token, Foundry checks the declared scope against the function's dependencies and their markings, and against the caller's access. The most common cause of failure is a missing marking: every marking on a resource in the scope must be allowed by the maximum authorization, if declared, and accessible to the caller.
The error lists each violation and names the resource, dependency, or marking involved.
| Cause | Fix |
|---|---|
| A resource in the scope has a marking that the maximum authorization does not allow | Add the marking to the maximum authorization, or omit max. For example, if you add pricingData to the scope and it is marked commercialData, token creation fails until the maximum authorization includes commercialData. The same applies to markings on resources that a called function reads. If the marking is already allowed, the caller lacks access to it; see the last row. |
| The calling function's maximum authorization does not include the markings that an action requires | Add the missing markings to the calling function's maximum authorization, or omit max. |
| The calling function runs with a marking that a called function's or action's maximum authorization does not include | Exclude the marking from the calling function, by removing it from the maximum authorization or removing the declared resource that requires it, or add the marking to the maximum authorization of the called function or action. The marking can come from a declared resource even when the calling function declares no max. |
| A resource in the scope is not imported into the repository's project | Add the resource named in the error as a project reference to the project named in the error. |
| A dependency requires stateful execution | Remove the dependency, including any indirect call to it. |
| The caller lacks access to a resource in the scope, or to a marking that a declared resource or dependency requires | Request access to the resource or marking. A function never runs with more access than its caller, so changing the scope does not grant the caller permissions. |
A created token confirms that the scope passed validation, not that it covers every resource the code accesses. Find the first failing API call in the function's error details or execution trace, then match it to one of the following causes:
| Cause | Fix |
|---|---|
| The code accesses a resource that is not in the scope | Add the resource to the scope. Reads of undeclared objects, datasets, or other resources are denied at runtime. |
| The code reads data with a marking that the maximum authorization does not allow, or that the caller cannot access | Add the marking to the maximum authorization and declare the resource that holds the data, or confirm that the caller can access the marking. |
| The caller lacks permission for the operation | Confirm that the user running the function can perform the operation on that resource. |
| The operation is not granted in scoped mode | Scoped mode grants read access to declared objects, links, interfaces, datasets, media sets, and streams, lets the function run declared models and call declared functions, and lets it apply declared actions. Any other write, such as writing to a dataset, is denied. To modify data, apply a declared action. |
| The application calls an older function version | Update the consuming application to use the version that contains the updated scope. |
After you update the scope, publish a new version and update the consuming application to use it.