Using the pycti Python client for OpenCTI

pycti is the official Python library for scripting OpenCTI, and it works best when a team pins versions, uses scoped service accounts and tests scripts against each platform release before upgrading.

On this page

When a team should use the pycti Python client for OpenCTI

Use the pycti Python client when a threat intelligence team needs to script work against OpenCTI that would be slow, error-prone or repetitive in the web application, such as bulk imports of indicators, applying markings to large sets of objects, or generating recurring reports. pycti is the official Python library for this job. It is the right tool when the work is a series of repeatable operations on data that already has a clear shape, and the team has at least one person comfortable maintaining Python code that runs on a schedule or on demand.

OpenCTI structures its data with a knowledge schema based on the STIX2 standards and exposes that data through two surfaces: a web application for analysts and a GraphQL API for programs. pycti sits on top of the API, so every call it makes is an API operation that the platform authorizes against the service account or token it was given. That makes the client a thin, convenient layer rather than a separate data path, and it means a script can do no more than the account behind it is permitted to do. Before writing code, confirm which API operations your team needs and whether the account's permissions and marking access cover them, since those limits apply to scripts exactly as they apply to people.

pycti covers API calls: creating and querying entities, setting markings and dates, and managing the requests your script sends. It does not cover packaging a connector that runs as a long-lived worker inside the platform's connector framework. Connector design, the official template and deployment belong to a separate topic, so if your need is an always-on service that imports from a source, plan for that work separately. Ask whether a one-off or scheduled script is enough for the job, and confirm the pycti version you will pin against the OpenCTI release you run, because the client and the platform must be kept in step.

Choosing how scripts reach OpenCTI

This table compares four ways code can reach OpenCTI: direct pycti calls from a script, raw GraphQL calls, pycti inside a connector, and a scheduled standalone job. Read each row against the work you need to run and the team that will maintain it. Every option calls the same platform API, so the upgrade check you run before a release applies wherever the code lives.

OptionBest fitMaintenance costUpgrade risk
Direct pycti calls from a scriptOne-off imports, bulk edits and reports run by an operatorScript owner tracks client changes and retests queriesClient and platform changes can both break calls; retest after each upgrade
Raw GraphQL calls over HTTPA few targeted queries, or a language with no maintained clientYou write, own and document every query and error pathSchema changes can break queries silently; review the schema after upgrades
pycti inside a connectorIngesting or enriching data within the connector lifecycleFollow the connector template and the platform's connector deployment modelTied to connector and client versions; test against the matching platform release
Standalone job using pyctiRecurring tasks that need their own schedule outside the connector modelYou run the scheduler, secrets, logging and failure alertsRuns on its own release cycle, so drift between job and platform is easy to miss
High-volume writesLarge batches of entities where asynchronous processing is acceptablePlan queue monitoring and throughput tests with your platform teamConfirm how your version routes writes through workers before relying on timing

Setting up pycti against a self-hosted OpenCTI platform

This sequence gives a team a scoped service account, a pinned pycti version matched to the platform release, and a verified connection before any script writes data. Each step closes with a check so the team knows the client is safe to use.

  1. Confirm the platform release and branch

    Before writing any code, record the exact OpenCTI version your platform runs, and whether it is a regular release or an LTS line. Regular releases use a x.YYMMDD.z version pattern and LTS releases add a -lts.n suffix, so the version string tells you which branch you are on. Check the version in the platform's own interface or release notes, and note it in your runbook. This is done when you have a written version string that every later step refers to.

  2. Create a dedicated service account with minimum roles

    Ask your OpenCTI administrator to create a separate user for scripts, not a personal account, and to attach a role that grants only the capabilities your script needs, such as reading and creating the entity types it touches. Keep the account's name clear, for example a name that identifies the script's purpose. Confirm in the platform's role settings that the account cannot administer users or change global configuration. Whether your edition supports the granular controls you need should be confirmed against the current OpenCTI documentation for your release, and the result is a service account whose permissions you can list on paper.

  3. Generate and store the API token

    Generate an API token for the service account from the platform, then store it in your secrets manager or an environment variable managed by your deployment tooling, never in source control. Record the token's owner and creation date in your inventory so it can be rotated later. Test that the token is not echoed in logs by your script runner. This step is complete when the token is available to the script at runtime and absent from code, configuration files and shell history.

  4. Pin the pycti version to the platform release

    Install the pycti package at a version that matches your OpenCTI release line, and pin it exactly in your requirements file or lock file. Match the major and minor release family to the platform, and confirm the pairing in the pycti release notes or the OpenCTI connector documentation before installing. Avoid upgrading pycti on its own when the platform has not changed. The check is a lock file entry that names one pycti version and a note in your runbook recording which platform release it was tested against.

  5. Connect to the platform URL with the token

    Create a client object that points to your platform's base URL and passes the service account token, using HTTPS and the certificate chain your organization trusts. Set a reasonable timeout and enable the library's logging at a level that does not print the token. Run the script from the same network segment and proxy configuration as your production jobs so the test reflects real access. The check is a clean connection with no authentication or certificate errors, and a log line showing the platform identifier or version the client reports.

  6. Run a read-only query before any write

    Query the platform for a small, known set of objects, such as a single page of one entity type, and confirm the response structure matches what your script expects. Keep the query narrow and avoid bulk pagination at this stage so the test does not load a busy platform. Compare the returned fields with the schema for your version. The step is done when the read succeeds, the count is plausible, and the returned data shows the service account can see only what its role allows.

  7. Create and remove a test entity

    Create one test entity in a non-production or clearly labeled namespace, applying the marking definition and the date fields your script will use, then read it back to confirm the markings and dates were stored as intended. Delete the test entity and confirm it is gone by querying for it again. Record the test entity identifier, the markings applied and the platform version in the test log. This step closes the sequence: the script is cleared for write operations only when the create, read back and delete checks all pass.

How markings and dates shape the entities your script creates

Set markings and dates deliberately whenever a script creates entities in OpenCTI, because the platform's data governance and organization segregation depend on them. A marking is an access or handling label attached to an object, and the platform uses those labels to decide who can see and share the data. If your script passes the marking definition IDs it should apply, such as TLP or organization markings, every object it creates inherits a known handling rule. Confirm the exact marking definition identifiers and the field names your pycti calls expect against the version you run, because marking behavior and the Enterprise Edition segregation features are tied to specific releases. Ask your platform owner which markings are mandatory for each source before the first write.

The most common mistake is an unmarked object. A script that omits markings may create entities that fall outside the handling rules your analysts expect, and cleaning them up later means finding every object the script touched. A second mistake is inconsistent date formats. Timestamps should follow the STIX 2.1 convention used by the platform, and a script that mixes time zones or string formats can produce objects that sort, filter or expire incorrectly. A third mistake is a first-seen or last-seen value that does not match the source. Those fields describe when the activity was observed, not when your script ran, so copying the import time into them misstates the intelligence. Map each date from the source record explicitly, and state in the script what happens when a source gives no date.

Test these settings on a non-production instance before any bulk run. Create a small set of sample objects with known markings and dates, query them back through the API, and compare the stored values with the source file. Check that the markings you expected are visible to the account and the group you intend, and that a user without that access cannot see them. Document the mapping in the script's repository so reviewers can confirm the handling logic without reading every line. Re-run this check after each platform upgrade, since field names and marking behavior can change between releases.

Keeping pycti scripts compatible with OpenCTI releases

OpenCTI's release model sets how often your scripts must change and how long each version stays supported. The four practices below work together: pin what runs, follow the support line, test before promotion, and read what changed.

Pin the platform and client versions

OpenCTI versions follow the pattern x.YYMMDD.z, so the number encodes the release date, and regular releases can ship from daily to weekly. Record the exact platform version each script was tested against. Install a matching pycti version from a locked requirements file so the installed package does not change without a review. Confirm with the client's own documentation which pycti releases support which platform versions before any upgrade.

Track the LTS line

LTS releases arrive every 6 months and each is supported for 1 year, so consecutive LTS versions overlap by 6 months. LTS builds use the x.YYMMDD.z-lts.n form and require a specific LTS license key to run. After a one-month stabilization period, the line receives only critical bug and security fixes, so scripts on it need a planned move to a newer line within its support window. Check the life cycle page for current dates.

Product life cycle docs

Regression test on staging

Run every script against a staging platform built from the target version before any production change, using representative data and a configuration close to production. Cover the paths that break most often: creating entities with marking definitions, setting first-seen and last-seen dates, reading relationships, and paging through large result sets. Keep test records in a dedicated test identity and marking so cleanup stays contained. Log the pass or fail result against each platform version so the next upgrade starts from evidence.

Watch release notes for API changes

Release notes are where changes to the API and client surface, and the releases page lists each version with its changes. Assign one owner to read notes for the LTS line and for any non-LTS line in use. Log every change to GraphQL fields, filters and client calls your scripts depend on, and treat a changed or removed field as a blocking item for the next upgrade. Confirm unclear changes with the maintainers before rollout.

OpenCTI releases

Questions teams ask before scripting OpenCTI with pycti

Can our own team script OpenCTI with pycti without a partner?

Yes, many teams do. A team with Python experience and an OpenCTI test instance can install the client, create a service account, and script basic creates and queries. The harder part is knowing which entities, markings and relationships your data model needs, and how to test changes safely. Ask whether your engineers have built against a STIX-based data model before, and whether you have a non-production platform to test against. If either answer is no, budget time for learning and for a test environment, or consider a partner for the first build.

What makes a pycti project take more effort?

Effort tracks the amount of logic you need to get right. The main drivers are:

  • The number of entity types and relationships your scripts create or update
  • The marking definition rules that decide who can see each object
  • Data volume and how often scripts run against a busy platform
  • How much test coverage you need before scripts touch production data
  • Whether scripts must handle dates, updates and deletions consistently across many sources

Each driver adds design, build and review work, so a short list of entity types with simple markings is far smaller than a multi-source workflow with strict access rules.

When should we bring in a partner for pycti scripting?

Bring in a partner when the scripts carry business or compliance weight and your team lacks time or prior experience with the platform. Typical triggers include scripts that write threat intelligence to a shared platform used by several teams, markings that must match a sharing agreement, or a need for a documented, tested handover. A partner can also review an existing script set before an OpenCTI upgrade. DeployOpen plans, builds, tests and documents self-hosted platform work in your infrastructure, with scope agreed in writing before work starts.

How do OpenCTI release upgrades affect our pycti scripts?

Upgrades can change the API and the client behavior your scripts depend on, so treat every platform upgrade as a test event. OpenCTI uses date-based version numbers, and non-LTS releases ship from daily to weekly, so the pace of change is high. LTS releases arrive every 6 months and are each supported for 1 year, which gives you a more predictable target. Match your pycti version to your platform version, keep a test instance on the target release, and rerun your script test suite before upgrading production. The OpenCTI product life cycle documentation sets out the LTS rules and is the place to confirm the current schedule.

Do we need an Enterprise Edition license to script OpenCTI?

Not for basic API and client use, but some features your scripts might depend on are Enterprise-only. The Enterprise Edition documentation lists features such as organization-based data segregation, audit logs and SSO configuration. Since OpenCTI 7.260224.0, defining and using authentication strategies, including SSO, is an Enterprise Edition feature. Check the licensing terms for each feature your scripts call, and confirm with Filigran which edition you need before you design access rules around them. LTS releases also require a specific LTS license key, so factor that into any production upgrade plan.

What should a quote for a pycti scripting project include?

A quote should follow from a written scope. Ask for the list of entity types and relationships in scope, the marking and access rules the scripts must respect, the sources and destinations of data, the test approach and environments, and the documentation and handover items. Ask which costs are one-time, such as design and build, and which recur, such as maintenance and upgrade testing after each OpenCTI release. Ask how changes to scope are handled and what the client must provide, such as a test instance, service account and sample data. DeployOpen does not publish a price list, and each quote follows from the agreed scope.

How do we keep script credentials and data access safe?

Use a dedicated service account with only the permissions each script needs, store its token outside source code, and rotate it on a schedule your security team sets. Use markings on every object your scripts create so that data is shared only with the intended groups. Log script activity and review it regularly. Confirm how your OpenCTI edition handles audit logs and segregation before relying on them for compliance evidence, since some of these controls are Enterprise Edition features.

Securing the API tokens and writes behind your scripts

A pycti script runs with whatever token it holds and writes whatever it creates, so the token, the data markings and the write volume all need controls before the script reaches production. The four cards below cover the main risks and the checks that keep a script from exposing or damaging shared threat intelligence.

Scope the service account

Every script should authenticate as its own service account with the smallest set of capabilities its job needs, not as an administrator. A reporting script rarely needs to create or delete indicators, so a read-only role for it limits the damage from a leaked token. Ask your platform team to list which capabilities each script account holds and to review that list whenever a script changes its behavior. Check that no script shares a token with a person or another script.

Rotate and revoke tokens

Tokens used by pycti should have an owner, a documented purpose and a rotation schedule that your security team agrees on. Revocation must be fast enough that a token found in a repository or log can be disabled without a redeploy of every consumer. Test the revocation path before you rely on it, and confirm with your administrator how tokens are issued and invalidated in your deployment. Record the rotation date next to the script's name in your inventory.

Keep secrets out of code

Hard-coded tokens in a Python file end up in version control, container images and shared notebooks. Load the OpenCTI URL and token from environment variables injected by a secrets manager at runtime, and keep the values out of logs, error messages and exception traces. Your scanners should check repositories for token-shaped strings before merge. Ask whether your secrets manager can issue short-lived credentials to scheduled jobs, since that changes how often rotation actually happens.

Mark and segregate every write

A script that creates entities without a marking definition or with the wrong organization can publish data to people who should not see it, and the error is hard to undo once other users have built on it. Set the TLP marking and the author or organization explicitly on each object the script creates, and reject writes that lack them. Test the markings on a sample of created objects after each change to the script. If your deployment uses organization-based segregation, confirm that the service account can only write to the organizations its script serves.

Batch and log bulk writes

A loop that creates thousands of objects one call at a time can slow the platform for analysts and fill its queues. Group creations into bounded batches, pause between them, and stop the run when error rates rise. Log every batch with its size, the count of successes and failures, and the script version, so a bad run can be traced and reversed. Agree a maximum batch size and run window with your platform owner before a large import, and check the queue and search index health during the first run.

How a pycti script reaches the OpenCTI platform

A pycti script starts in your own network zone and calls the OpenCTI GraphQL API over the platform's web endpoint, which is the trust boundary to review first. Reads return directly from the platform, while writes are queued through RabbitMQ and applied by Python workers before landing in the search index and object storage. Check which zone the script runs in, which service account it uses, and whether the platform can reach its dependencies as documented.

Request path from a pycti script through the API, RabbitMQ and workers to storage.

How DeployOpen scopes and delivers pycti automation

DeployOpen is a Chainscore Labs company that plans, implements, integrates and supports self-hosted open-source platforms inside your own infrastructure, including OpenCTI. For pycti work, we start from a written scope that names the entities, markings and automations in play, then build and test scripts against your own OpenCTI instance before handing them over.

How we scope

  • Engagements are fixed-scope, with deliverables and timelines agreed in writing before work starts.
  • The written scope lists the entity types, relationships, markings and date fields your scripts must handle.
  • We confirm the OpenCTI version and LTS line your platform runs, so the pycti version is pinned to match it.

How we build

  • Scripts use a service account with permissions limited to the objects and actions the automation needs.
  • We test create, query and update paths against a non-production OpenCTI instance before any production run.
  • Marking definitions and date handling are checked against sample data from your own sources.

How we hand over

  • You receive the source code, pinned dependency versions and configuration documentation for your team to maintain.
  • Runbooks explain how to repeat a test run, rotate the service account credential and roll back a change.
  • We document which pycti release each script was tested against and what to recheck before an upgrade.

How we support

  • Ongoing support, upgrades and continuous testing are available as retainers after the initial handover.
  • Retainer work can include retesting scripts when your OpenCTI platform moves to a new release or LTS line.
  • Your team confirms the scope of any follow-on change in writing before it is built.

Sources and further reading

Talk to our team.

Tell us what you're working on, whether it's a deployment, an audit, a security test or a cyber range. You'll speak with an engineer who can help you scope it.

  • 30-minute call: free, with no obligation.
  • NDA on request: we can sign before you share details.
  • Clear next steps: a scope and plan after the call.