Installation and Setup

  1. Add schematichq to your project’s build file:
$pip install schematichq
># or
>poetry add schematichq
  1. Issue an API key for the appropriate environment using the Schematic app.

  2. Using this secret key, initialize a client in your application:

1from schematic.client import Schematic
2
3client = Schematic("YOUR_API_KEY")

Async Client

The SDK also exports an async client so that you can make non-blocking calls to our API.

1from schematic.client import AsyncSchematic
2
3client = AsyncSchematic("YOUR_API_KEY")
4async def main() -> None:
5 await client.companies.get_company(
6 company_id="company_id",
7 )
8
9asyncio.run(main())

Exception Handling

All errors thrown by the SDK will be subclasses of ApiError.

1try:
2 client.companies.get_company(
3 company_id="company_id",
4 )
5except schematic.core.ApiError as e: # Handle all errors
6 print(e.status_code)
7 print(e.body)

Usage examples

Sending identify events

Create or update users and companies using identify events.

1from schematic import EventBodyIdentifyCompany
2from schematic.client import Schematic
3
4client = Schematic("YOUR_API_KEY")
5
6client.identify(
7 keys={
8 "email": "wcoyote@acme.net",
9 "user_id": "your-user-id",
10 },
11 company=EventBodyIdentifyCompany(
12 keys={"id": "your-company-id"},
13 name="Acme Widgets, Inc.",
14 traits={
15 "city": "Atlanta",
16 },
17 ),
18 name="Wile E. Coyote",
19 traits={
20 "login_count": 24,
21 "is_staff": false,
22 },
23)

This call is non-blocking and there is no response to check.

Sending track events

Track activity in your application using track events; these events can later be used to produce metrics for targeting.

1from schematic.client import Schematic
2
3client = Schematic("YOUR_API_KEY")
4
5client.track(
6 event="some-action",
7 user={"user_id": "your-user-id"},
8 company={"id": "your-company-id"},
9)

This call is non-blocking and there is no response to check.

Creating and updating companies

Although it is faster to create companies and users via identify events, if you need to handle a response, you can use the companies API to upsert companies. Because you use your own identifiers to identify companies, rather than a Schematic company ID, creating and updating companies are both done via the same upsert operation:

1from schematic.client import Schematic
2
3client = Schematic("YOUR_API_KEY")
4
5client.companies.upsert_company(
6 keys={"id": "your-company-id"},
7 name="Acme Widgets, Inc.",
8 traits={
9 "city": "Atlanta",
10 "high_score": 25,
11 "is_active": true,
12 },
13)

You can define any number of company keys; these are used to address the company in the future, for example by updating the company’s traits or checking a flag for the company.

You can also define any number of company traits; these can then be used as targeting parameters.

Creating and updating users

Similarly, you can upsert users using the Schematic API, as an alternative to using identify events. Because you use your own identifiers to identify users, rather than a Schematic user ID, creating and updating users are both done via the same upsert operation:

1from schematic.client import Schematic
2
3client = Schematic("YOUR_API_KEY")
4
5client.companies.upsert_user(
6 keys={
7 "email": "wcoyote@acme.net",
8 "user_id": "your-user-id",
9 },
10 name="Wile E. Coyote",
11 traits={
12 "city": "Atlanta",
13 "high_score": 25,
14 "is_active": true,
15 },
16 company={"id": "your-company-id"},
17)

You can define any number of user keys; these are used to address the user in the future, for example by updating the user’s traits or checking a flag for the user.

You can also define any number of user traits; these can then be used as targeting parameters.

Checking flags

When checking a flag, you’ll provide keys for a company and/or keys for a user. You can also provide no keys at all, in which case you’ll get the default value for the flag.

1from schematic.client import Schematic
2
3client = Schematic("YOUR_API_KEY")
4
5client.check_flag(
6 "some-flag-key",
7 company={"id": "your-company-id"},
8 user={"user_id": "your-user-id"},
9)

Advanced

Flag Check Options

By default, the client will do some local caching for flag checks. If you would like to change this behavior, you can do so using an initialization option to specify the max size of the cache (in terms of number of entries) and the max age of the cache (in milliseconds):

1from schematic.client import LocalCache, Schematic
2
3cache_size = 100
4cache_ttl = 1000 # in milliseconds
5config = SchematicConfig(
6 cache_providers=[LocalCache[bool](cache_size, cache_ttl)],
7)
8client = Schematic("YOUR_API_KEY", config)

You can also disable local caching entirely; bear in mind that, in this case, every flag check will result in a network request:

1from schematic.client import Schematic
2
3config = SchematicConfig(cache_providers=[])
4client = Schematic("YOUR_API_KEY", config)

You may want to specify default flag values for your application, which will be used if there is a service interruption or if the client is running in offline mode (see below):

1from schematic.client import Schematic
2
3config = SchematicConfig(flag_defaults={"some-flag-key": True})
4client = Schematic("YOUR_API_KEY", config)

Offline Mode

In development or testing environments, you may want to avoid making network requests to the Schematic API. You can run Schematic in offline mode by specifying the offline option; in this case, it does not matter what API key you specify:

1from schematic.client import Schematic
2
3config = SchematicConfig(offline=True)
4client = Schematic("", config)

Offline mode works well with flag defaults:

1from schematic.client import Schematic
2
3config = SchematicConfig(
4 flag_defaults={"some-flag-key": True},
5 offline=True,
6)
7client = Schematic("", config)
8client.check_flag("some-flag-key") # Returns True

Timeouts

By default, requests time out after 60 seconds. You can configure this with a timeout option at the client or request level.

1from schematic.client import Schematic
2
3client = Schematic(
4 # All timeouts are 20 seconds
5 timeout=20.0,
6)
7
8# Override timeout for a specific method
9client.companies.get_company(..., {
10 timeout_in_seconds=20.0
11})

Retries

The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long as the request is deemed retriable and the number of retry attempts has not grown larger than the configured retry limit (default: 2).

A request is deemed retriable when any of the following HTTP status codes is returned:

  • 408 (Timeout)
  • 429 (Too Many Requests)
  • 5XX (Internal Server Errors)

Use the max_retries request option to configure this behavior.

1# Override timeout for a specific method
2client.companies.get_company(..., {
3 max_retries=1 # Only retry once on failure
4})

Custom HTTP client

You can override the httpx client to customize it for your use-case. Some common use-cases include support for proxies and transports.

1import httpx
2
3from schematic.client import Schematic
4
5client = Schematic(
6 http_client=httpx.Client(
7 proxies="http://my.test.proxy.example.com",
8 transport=httpx.HTTPTransport(local_address="0.0.0.0"),
9 ),
10)