Plex API Access

Updated Dec 3, 2026
DataMagik Automate

Three ways to reach Plex

Scripts and workflows can talk to Plex in three ways. Each uses its own kind of credential, and each is good at a different job.

Plex data sourcesPlex Connect REST APIPlex ODBC (SQL)
Script functionplex.execute()plex.api.call()odbc.executeQuery()
Workflow stepPlex Data SourcePlex APIODBC Query
Credential typeBasic Auth (Plex web services username and password)Plex Connect (REST API) (Consumer Key)ODBC Connection (Plex ODBC access token)
ReadsYesYesYes
WritesYes, through data sources that writeYes, through POST, PUT, PATCH and DELETE operationsNo. Plex ODBC is read-only.
Best forPlex transactions and the reads Plex already packages as a data sourceCustomers, sales orders, inventory, EDI and other records Plex Connect exposesReports and reads that join many tables or return many rows

Set up the credentials under Script Engine › Credentials; see Script Credentials & Allowed Domains.

Which one to use

  • To change data in Plex, use a data source that does the job (plex.execute). If there is none, use a Plex Connect operation (plex.api.call). SQL cannot write.
  • To read a lot of rows, or to combine several tables, use a saved ODBC query.
  • To read one record or a short list, use whichever already gives you the answer: a data source, a Connect operation, or a query.
  • Never call Plex with fetch(). The helpers build the URL, add the credential, retry when Plex is busy and cap the response size. A hand-written fetch() to a Plex host gets none of that, and it is subject to the same access checks as the helpers.

Plex data sources: plex.execute

Finding a data source

Open Script Engine › Plex Data Sources. Describe what you want in plain words, such as "book scrap against a workcenter" or "open purchase orders", and click Search.

  • Choose an action (Read, Report, Create, Update or Delete) to get recommendations for that kind of task instead of plain matches. Any action searches everything.
  • Tick Only data sources that cannot change anything to hide the ones that write.
  • Each result is marked read-only, WRITES, DELETES or EXTERNAL CALL. Check this before you call one: a data source that writes looks like any other in a list.

The note above the results says what they were filtered against. When it says the results are filtered to what your company's Plex credential is granted, you can call them. When it says they are filtered on what Plex allows in general, some of them (around 40%) may not be enabled for your PCN.

Click a result to see its contract: When to use, What it does, Returns, its Inputs (required ones are marked) and Outputs, the tables it reads and writes, any Caveats, and a Call it from a script snippet with its data source ID filled in.

Calling it

OptionRequiredWhat it does
credentialNameYesThe name of a Basic Auth credential holding your Plex web services login.
dataSourceIdYesThe data source's ID, as a string, such as "11887".
companyCodeNoYour Plex company code. It picks your company's own Plex address. Letters, numbers and hyphens only.
isTestNotrue sends the call to your Plex test environment. The default is false: production.
bodyNoThe data source's inputs, as an object.

The Plex address is built for you from companyCode and isTest:

companyCodeisTest: falseisTest: true
Set, such as acmeacme.on.plex.comacme.test.on.plex.com
Not setcloud.plex.comtest.cloud.plex.com
function main(context) {
  const result = plex.execute({
    credentialName: "PLEX_BASIC_AUTH",
    companyCode: "acme",
    dataSourceId: "11887",
    body: { Workcenter_Code: context.workcenter }
  });
  if (!result.success) {
    return { success: false, script_message: "Plex data source 11887 failed (HTTP " + result.status + "): " + result.error };
  }
  const rows = result.data.rows || [];
  return { success: true, data: { count: rows.length, rows } };
}

plex.execute returns { success, status, data, error, retries }; it does not throw. data is what Plex answered: a read puts its rows in data.rows, and a write puts its output values, such as Result_Code, in data.outputs. If Plex answers with something that is not JSON, success is still true and the text is in data.raw.

Retries and limits

  • When Plex answers 429 (too many requests) or a 5xx error, the call is retried up to 3 times, waiting 1, 2 and then 4 seconds, or as long as Plex asks (up to 30 seconds). retries says how many retries it took.
  • Each request may take up to 60 seconds.
  • A response larger than 10 MB fails the call and is not retried. Narrow the inputs so less comes back.

Plex Connect REST API: plex.api.call

Plex Connect is Plex's modern REST API at connect.plex.com. It is separate from data sources: a different address, a different credential, and named operations instead of numeric IDs.

The credential

Create a Plex Connect (REST API) credential with the Consumer Key (API Key) of your app in the Plex Developer Portal. Fill in Default PCN and Default Tenant Id if calls should go to a particular Plex tenant unless a script says otherwise.

Finding an operation

In the script editor, open the Documentation panel (the book button) and choose the Plex APIs tab. Search for a record or an action, such as "customer" or "create order". Each operation shows its method and URL, an Input tab with the fields it takes and an Output tab with what it returns. Copy and Insert give you a ready-made plex.api.call for it.

In a workflow, the Plex API step has the same search built in.

Calling it

OptionRequiredWhat it does
credentialNameYesThe name of a Plex Connect (REST API) credential. There is no default.
product, operationTogetherThe API product and operation, such as customers-api and create-customer. The method and URL are looked up for you.
pathParamsNoValues for {id}-style parts of the URL.
queryNoQuery string values. An array repeats the parameter.
bodyNoThe request body for POST, PUT and PATCH.
pcn, tenantIdNoSend this call to a specific Plex tenant. Leave them out to use the credential's defaults.
isTestNotrue sends the call to test.connect.plex.com.
method, pathNoInstead of product and operation: call a method and path directly, such as GET /mdm/v1/employees.
function main(context) {
  const res = plex.api.call({
    credentialName: "PLEX_CONNECT",
    product: "customers-api",
    operation: "create-customer",
    body: { code: "ACME01", name: "Acme Co", status: "Active" }
  });
  if (!res.success) {
    return { success: false, script_message: res.method + " " + res.url + " failed: " + res.error };
  }
  return { success: true, data: res.data };
}

plex.api.call returns { success, status, data, error, method, url, credential, retries }. method, url and credential say exactly what was called and with which credential, which is useful in a failure message. Some operations return an object and others a list, so check what data holds. It retries on 429 and 5xx errors in the same way as plex.execute.

Plex ODBC: SQL queries

The credential

Create an ODBC Connection credential with your Plex ODBC Username and Access Token (PAT). The Host and Port default to odbc.plex.com and 19995. Tick Set as default for this credential type on the one most queries should use.

A query runs with, in order: the credential named by credentialId in the call's options; the credential saved with the query; the credential named by credential in the options; or the company's default ODBC credential. A saved query that points at one of your own databases (see SQL Editor) runs there instead, through the on-prem connector.

Save the query first

Write and test the SQL in Script Engine › SQL Editor, then save it with a Query Name and, if it needs longer than usual, a Default Timeout (seconds). Put values the script will supply as @name placeholders. Saved queries can be tested on their own, shared between scripts and corrected without a new script version. See SQL Editor.

function main(context) {
  try {
    const result = odbc.executeQuery("Open POs by Supplier", { supplier_code: context.supplier });
    return { success: true, data: { count: result.rowCount, rows: result.rows } };
  } catch (e) {
    return { success: false, script_message: "Query failed: " + e.message };
  }
}

odbc.executeQuery returns { success, columns, rows, rowCount, executionMs, requestId }. Each row is an object keyed by column name. A query that finds nothing returns normally with no rows.

A failed query throws; it does not return success: false. The error carries errorCode (such as TIMEOUT or SYNTAX) and requestId. Wrap the call in try/catch when you want to handle the failure yourself.

Things to know

  • A misspelled query name runs as SQL. The first argument is looked up as a saved query name, then as a saved query ID. If neither matches, it is sent to Plex as SQL, so a typo comes back as a syntax error naming the first word of the name.
  • Timeouts: pass { timeout: seconds } as the third argument. The default is 30 seconds (or the saved query's own timeout), and the most is 300.
  • Raw SQL: odbc.execute(sql, params) runs SQL written in the script. Use it for experiments; for anything that runs in production, save the query.
  • Running queries in parallel: odbc.executeQueryAsync returns a Promise and lets several queries run at once with Promise.all in an async function main. Unlike the plain version, it resolves with { success: false, error } when a query fails.

The SQL dialect

Plex ODBC is a DataDirect OpenAccess engine, not SQL Server. Much of T-SQL works, but some common forms do not:

Not supportedWrite instead
TOP (100), LIMIT, OFFSET … FETCHSELECT TOP 100 … (no parentheses). Page with a WHERE on the sort key.
CTEs (WITH x AS (…))A derived table: FROM (SELECT …) x
CROSS APPLY, OUTER APPLY, CROSS JOINA join to a grouped derived table
SUM(…) OVER (…) and other aggregate window functions; LAG, LEADA correlated subquery. ROW_NUMBER, RANK and DENSE_RANK do work.
IIFCASE WHEN … THEN … ELSE … END
CONVERT, TRY_CASTCAST(x AS type). Use TIMESTAMP, not DATETIME.
DATEPART, DATENAMEYEAR(x), MONTH(x), DAY(x)
EXCEPT, INTERSECTWHERE NOT EXISTS (…), WHERE EXISTS (…)
#temp tables, DECLARE, WITH (NOLOCK)A derived table, a literal or a named parameter; drop the hint.
INSERT, UPDATE, DELETENothing: Plex ODBC is read-only. Change data with a data source or a Plex Connect operation.

Who can call Plex

If your company has turned on Enforcement on the SQL Access Roles page, access to Plex depends on roles:

  • ODBC queries are checked against the Plex databases, tables and PCNs the roles allow. A query that goes outside them is refused before it runs.
  • plex.execute, plex.api.call, and any fetch() to a Plex address are refused when the person running the script is not permitted, but only when the script is run directly: from the editor, or through the API. Scripts started by a schedule, an automation, an app or a dashboard are not limited this way.
  • In a Site, Plex access is always enforced. Users of the parent company count the roles checked on the parent's Plex API Access page. So do people given access to the parent from another company, such as a partner consultant or DataMagik staff, while that access lasts and if they hold DataMagik - Security at the parent. Everyone else needs an allowed role in the Site itself.

From Claude: running a Plex data source through the DataMagik connector in Claude needs the DataMagik - Viewer or DataMagik - Builder role, as running ODBC SQL queries from Claude does. See Minimum access for using Claude.

Calls per run

There is no limit on how many calls one run of a script makes through the Script Engine's helpers, Plex calls included. Each call is counted, and the count shows on the run in History. What bounds a run is its timeout (30 seconds on a new script, up to 2700 seconds). A job that needs longer should work in batches across runs; see the Checkpoint section of the editor's Documentation panel.

Related pages

Was this page helpful?