Skip to main content

SWS API Client

This library provides the user with a set of useful tools to easily interact with the FAO SWS (Statistical Working System) REST APIs.

Campaign validation outputs

Campaign files use top-level visibility: shared | internal, defaulting to shared. Pass visibility="internal" to create_file before uploading internal content. CampaignFile reads visibility and validation_report_id; RunOutput uses visibility instead of audience. The migration is audience="both" → visibility="shared", and audience="internal" → visibility="internal".

register_validation_output(team_id, report_id, source_file_id, descriptor) registers an internal artifact of the exact report. validation_outputs lists it and download_content_to_file streams it. Every manual harvesting event names one file. Worker methods claim_manual_harvest, heartbeat_manual_harvest, manual_harvest_child_task and complete_manual_harvest use the file ID, request ID and wrapper task ID. Record the child immediately after launch; completion has one outcome. Statistical writes use the existing Datasets.import_file_via_plugin and Datatables.import_file_via_plugin methods to start separate Save Data tasks.

The packaged report schema supports a DATA ZIP with root data.csv and optional observation metadata.csv, plus explicit separator/quote options. METADATA outputs are independent datatable inputs; separate report files are not implicitly paired.

Pass payload.cycleId from ValidationTriggered to complete_validation(cycle_id=...), including when the plugin fails. This lets CM reject an old run's completion after a validation restart.

Installation

The module is available on Pypi:

python -m pip install sws_api_client

The library requires Python 3.9+.

Modules

Beside the SWS APIs, the client covers the Campaign Manager — the platform that runs FAO's questionnaire campaigns. CampaignManager is what a Task Manager wrapper needs and nothing more: claim a generation run, register a questionnaire and put its bytes on it, claim a check or a harvest, keep the claim alive with a heartbeat, and report how it went.

from sws_api_client import SwsApiClient, CampaignManager, ValidationSummary, in_minutes

cm = CampaignManager(SwsApiClient.auto())
cm.claim_validation(team_id=4, file_id=812, task_id="task-1", expires_at=in_minutes(15))
report = cm.create_file(team_id=4, campaign_id=7, name="report-812", type="validationReport")
cm.upload_content(team_id=4, file_id=report.id, path="report.html")
cm.complete_validation(
    team_id=4, file_id=812, outcome="success", file_report_id=report.id,
    summary=ValidationSummary(checks_run=12, passed=12, failed=0),
)

The endpoint is resolved through /discover (key campaigns_api) and the credentials travel on every request exactly as they do for the other SWS APIs — there is nothing to configure per call.

Usage

To use the package the user needs to create an instance of the SwsApiClient class, provide it with the necessary parameters and execute the methods to query the specific endpoints.

Instantiate the client locally

There are three methods to instantiate the client:

1. Pass the sws_endpoint and the access_token to the constructor

from sws_api_client import SwsApiClient

sws_client = SwsApiClient(sws_endpoint="<sws_endpoint>", access_token="<access_token>")

2. Pass to sws_endpoint and the access_token as named arguments

We need to execute the script from command line passing --sws_endpoint and --access_token as arguments:

python script.py --sws_endpoint <endpoint> --access_token <test_access_token>

And instantiate the client in our script with the class method from_args:

from sws_api_client import SwsApiClient

sws_client = SwsApiClient.from_args()

3. Create a conf file where to store the arguments

We need to create a conf file (default name: "conf_sws_api_client.json") with the following structure:

{
    "sws_endpoint": "https://sws.dev.fao.org",
    "sws_token": "XXX",
    "current_task_id": "XXX",
    "current_execution_id": "XXX",
    "authclient": {
        "clientId": "XXX",
        "clientSecret": "XXX",
        "tokenEndpoint": "https://fao-dev.auth.eu-west-1.amazoncognito.com/oauth2/token",
        "scope": "sws/user"
    }
}

And instantiate the client in our script with the class method auto:

from sws_api_client import SwsApiClient

sws_client = SwsApiClient.auto()

Behind the scene it will automatically detect the fact that you are in debug mode and it will instanciate the client using the from_conf method

Instantiate the client in a SWS plugin

When working withing a SWS plugin instantiate the client as:

from sws_api_client import SwsApiClient

sws_client = SwsApiClient.auto()

Behind the scene it will automatically detect the fact that you are in a SWS plugin and it will instanciate the client using the from_env method

Instantiate the client using env variables

You can also instantiate the client using the following environment variables:

export SWS_ENDPOINT=https://sws.fao.org
export SWS_TOKEN=XXX
export SWS_USER_CREDENTIALS_SECRET_NAME=dev/sws/user_client

If you have no access to the SWS_USER_CREDENTIALS_SECRET_NAME you can use the following environment variables:

export SWS_AUTH_CLIENTID=XXX
export SWS_AUTH_CLIENTSECRET=XXX
export SWS_AUTH_TOKENENDPOINT=XXX
export SWS_AUTH_SCOPE=XXX

And instantiate the client in our script with the class method from_env:

from sws_api_client import SwsApiClient

sws_client = SwsApiClient.from_env()

Access different API Services

The library provides a set of classes to interact with the different services available in the SWS API. The available classes are:

  • Tags to interact with the SWS Tags API
  • Datasets to interact with the Datasets API provided by both the legacy SWS IS Api and the new Session API
  • Tasks to interact with the SWS TaskManager API
  • Datatable to interact with the SWS Datatable API
  • Plugins to interact with the SWS Plugins API

Perform requests

To perform requests you just need to call the available methods using the SwsApiClient object, as an example:

datasets = Datasets(sws_client)
dataset = datasets.get_dataset_info('aproduction')
logger.info(f"Dataset info: {dataset}")

or to get the list of tags:

tags = Tags(sws_client)
tags_list = tags.get_tags()
logger.info(f"Tags: {tags_list}")

more complete examples can be found in the example folder:

cp example/conf_sws_api_client.json.example example/conf_sws_api_client.json
# edit the file with your credentials
python3 -m venv example/.venv
source example/.venv/bin/activate
pip install -r example/requirements
python example/dataset_example.py

If you need to perform a test request not in debug mode, you can use the following command:

AWS_PROFILE=fao-dev DEBUG_MODE=FALSE SWS_USER_CREDENTIALS_SECRET_NAME=dev/sws/user_client SWS_TOKEN=YOUR_TOKEN SWS_ENDPOINT=https://sws.dev.fao.org python3 dataset_example.py

Development

Please follow the semantic release commit message format.

Branches

develop

The develop branhces is the main branch for development. All feature branches should be created from this branch any commit to the development branch will create an alpha version.

feature/*

The feature/* branches are the branches for new features. Any commit to a feature branch will create a beta version.

main

The main branch is the production branch. Any commit to the main branch will create a release version.

Release

Migrated to github test 6

Metadata

Release files for sws-api-client 3.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sws-api-client 3.0.0
File Size Uploaded
sws_api_client-3.0.0.tar.gz 78.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sws-api-client 3.0.0
File Interpreter ABI Platform
sws_api_client-3.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 170.2 kB

Release files / sws_api_client-3.0.0.tar.gz

Download URL sws_api_client-3.0.0.tar.gz
Size 78.7 kB
Tags Source
SHA-256 checksum
How to use checksums
2d034e3c6836686659ee2d2f59aa15820662d2341d025be65f4fa806864cf3fc
BLAKE2b-256 checksum
How to use checksums
559bbed024c87c3dc63974de8603921a7983e0da4112beacf9ddafa8000f878a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 7, 2026.

Transparency log

Release files / sws_api_client-3.0.0-py3-none-any.whl

Download URL sws_api_client-3.0.0-py3-none-any.whl
Size 91.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8c288ff62a12d4c6e6e95b477d1bab71ac603f840e34c3cada80617fb34d9a42
BLAKE2b-256 checksum
How to use checksums
6b80cce51a4246db4ced041a689e138e471fe5fb70dec9ff3990d2b545df158d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 7, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

3.0.0 This release

2 release files

2.26.0

2 release files

2.25.0

2 release files

2.24.0

2 release files

2.23.1

2 release files

2.23.0

2 release files

2.22.2

2 release files

2.15.0

2 release files

2.13.0

2 release files

2.12.0

2 release files

2.11.0

2 release files

2.10.0

2 release files

2.9.1

2 release files

2.7.3

2 release files

2.7.2

2 release files

2.7.1

2 release files

2.7.0

2 release files

2.6.0

2 release files

2.5.4

2 release files

2.5.3

2 release files

2.5.2

2 release files

2.5.1

2 release files

2.5.0

2 release files

2.4.0

2 release files

2.3.0

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.5.12

2 release files

1.5.11

2 release files

1.5.9

2 release files

1.5.4

2 release files

1.5.3

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4.6

2 release files

1.4.5

2 release files

1.4.4

2 release files

1.4.3

2 release files

1.3.2

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.14

2 release files

1.0.13

2 release files

1.0.12

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page