Skip to main content

Python client for Sanity.io CMS HTTP API

Project description

python-sanity

Python client for Sanity.io CMS HTTP API.

ℹ️ Note: This package is an active fork of the original project at OmniPro-Group/sanity-python.

Install

Available on pypi as package python-sanity

Install with uv:

uv add python-sanity

Install with pip:

pip install python-sanity

Environment Variables

You can pass parameters to the client constructor directly, but it is recommended to use environment variables.

Variable Description Required Default Value
SANITY_PROJECT_ID The project ID Yes
SANITY_DATASET The dataset to use No production
SANITY_API_TOKEN The API token Yes
SANITY_LOG_LEVEL Level of logging No INFO

Examples

from sanity.client import Client
import logging
from scripts.colour_json import print_json_in_colour

logger = logging.getLogger(__name__)

project_id = "<project id>"
dataset = "<dataset>"
token = "<api token>"

client = Client(
    logger,
    project_id=project_id,
    dataset=dataset,
    token=token,
    use_cdn=True
)

# GET Query Method
result = client.query(
    groq="count(*[_type == 'post'])",
    explain=False,
    variables={
        "language": "es",
        "t": 4
    },
    method="GET"
)
print_json_in_colour(result)

# POST Query Method
result = client.query(
    groq="count(*[_type == 'post'])",
    variables={
        "language": "es",
        "t": 4
    },
    method="POST"
)
print_json_in_colour(result)

# Assets
png = "https://some.web.address.com/some_name.png"
result = client.assets(file_path=png)
print_json_in_colour(result)

png2 = "some_file_path/name"
result = client.assets(file_path=png2, mime_type="image/png")
print_json_in_colour(result)

# Mutate
transactions = [
    {
        "createOrReplace": {
            "_id": "speaker.asdf",
            "_type": "speaker",
            "title": "Some Name",
            "slug": {
                "_type": "slug",
                "current": "some-name"
            },
            'image': {
              '_type': 'image',
              'asset': {
                '_ref': 'image-6ffb37d2eeabc7d07b3ca485c7b497e77bdcccd4-1232x1280-png',
                '_type': 'reference'
              }
            }
        }
    }
]
result = client.mutate(
    transactions=transactions,
    return_ids=False,
    return_documents=False,
    visibility="sync",
    dry_run=False,
)
print_json_in_colour(result)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_sanity-0.1.2.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_sanity-0.1.2-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file python_sanity-0.1.2.tar.gz.

File metadata

  • Download URL: python_sanity-0.1.2.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.2

File hashes

Hashes for python_sanity-0.1.2.tar.gz
Algorithm Hash digest
SHA256 340e07918d16a72cb0d84c54aab1ff9c7fda173a5589f148b0eafe78dde0463c
MD5 e2f271fc94d34a759f3b7d1ad519a664
BLAKE2b-256 bc62780f3839fc13ee0a37cea8cbb07f2c33ca80951f112b30dbcfb79c754964

See more details on using hashes here.

File details

Details for the file python_sanity-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for python_sanity-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f67ce45a2b42b7a62927bc7f1ee490b6741f88db44f74570a5445c13bd391908
MD5 d31083a52a3e25004b90b6466cc3c3f6
BLAKE2b-256 b8e631b178ba7d24c1a6900f1065b61670fdbdd33196dc276a375bb03c8d5961

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page