Home → Blog

Shanghai congestion data can be useful for monitoring port activity, but it cannot tell you how long a vessel is waiting at anchor right now. By the end of this guide, you can request the CNSHA series, read its activity fields, and display the observation date without presenting lagged port-call data as live AIS wait times.

What the Shanghai congestion reading measures

The port-congestion endpoint draws on IMF PortWatch data. The series is typically lagged by about 6–7 days, so a response retrieved today describes an earlier observation rather than a live view of Shanghai traffic. Check as_of in every result instead of treating the request time as the observation time.

Illustration: Shanghai Congestion Today

The underlying comparison is the average daily port-call volume over the last 7 days against a 90-day baseline for the same port. The ratio_vs_baseline field expresses that comparison as a dimensionless ratio: a value below 1 means the recent average is below the baseline, while a value above 1 means it is above the baseline.

This is an activity measure, not a direct measurement of vessel waiting time or anchorage duration. Fewer calls than usual can reflect a closure or ships waiting offshore, among other circumstances; the activity figure alone does not establish which explanation applies. In the response methodology, congestion is a deprecated alias of activity, so new integrations should prefer activity when it is available.

Request the Shanghai record

Use CNSHA as the port query value and supply your key in the X-API-Key header. Guests without a key are limited to 100 requests per day per IP.

curl -H "X-API-Key: YOUR_API_KEY" "https://freightpulsehq.com/api/v1/port-congestion?port=CNSHA"

The following sample is a dated observation, not a current wait-time quote. Its as_of value and metadata should be read alongside the activity fields.

{
    "success": true,
    "data": {
        "results": [
            {
                "port": "Shanghai",
                "locode": "CN SGH",
                "country": "China",
                "activity": "moderate",
                "congestion": "moderate",
                "as_of": "2026-11-20",
                "metrics": {
                    "recent_avg_portcalls_per_day": 71.6,
                    "baseline_avg_portcalls_per_day": 83.7,
                    "ratio_vs_baseline": 0.85
                }
            }
        ],
        "global_summary": {
            "ports_high_activity": 0,
            "ports_moderate_activity": 1,
            "ports_low_activity": 0,
            "ports_high_congestion": 0,
            "ports_moderate": 1,
            "ports_low": 0
        },
        "meta": {
            "source": "IMF PortWatch (portwatch.imf.org) - UN Global Platform AIS data",
            "data_lag_days": "4-11 (PortWatch republishes the daily series once a week; check as_of)",
            "methodology": "activity is port-call volume, not congestion: last 7 days' avg calls vs a 90-day baseline for the same port. low means fewer calls than usual, which can be a closure or ships waiting offshore. It does not measure vessel wait or anchorage time. congestion is a deprecated alias of activity."
        }
    },
    "meta": {
        "request_id": "a6f8593f-13a0-482e-9316-f792eefd7063"
    }
}

Read success before using data.results. Within the Shanghai result, activity is the classification, as_of is the observation date, and metrics contains the recent and baseline averages in port calls per day plus the unitless ratio. The sample returns both activity and the deprecated congestion alias.

The response metadata describes a 4–11-day lag range and says the daily series is republished once a week; the typical lag is approximately 6–7 days. Those statements make as_of more useful than a fixed assumption about freshness. The sample’s returned locode differs in form from the CNSHA query value, so retain the requested port identifier and the returned value separately rather than silently treating them as identical strings.

Read the fields in Python

This example uses the Python standard library, calls the same endpoint, and prints the fields needed to label the reading. Replace YOUR_API_KEY with your own key before running it; keep the key out of source control in a deployed application.

import json
import sys
from urllib.error import HTTPError, URLError
from urllib.request import Request, urlopen

url = "https://freightpulsehq.com/api/v1/port-congestion?port=CNSHA"
request = Request(url, headers=dict([("X-API-Key", "YOUR_API_KEY")]))

try:
with urlopen(request) as response:
payload = json.load(response)
except HTTPError as exc:
sys.exit("HTTP request failed with status: " + str(exc.code))
except URLError as exc:
sys.exit("Could not reach the API: " + str(exc.reason))
except (ValueError, UnicodeDecodeError):
sys.exit("The response was not valid JSON.")

if not isinstance(payload, dict) or payload.get("success") is not True:
sys.exit("The API did not return a successful response.")

data = payload.get("data")
if not isinstance(data, dict):
sys.exit("The response did not contain a data object.")

results = data.get("results")
if not isinstance(results, list) or not results:
sys.exit("No Shanghai result was returned.")

result = results[0]
if not isinstance(result, dict):
sys.exit("The result was not an object.")

metrics = result.get("metrics")
if not isinstance(metrics, dict):
metrics = dict()

print("Requested port: CNSHA")
print("Returned port:", result.get("port"))
print("Returned locode:", result.get("locode"))
print("Activity:", result.get("activity"))
print("Congestion alias:", result.get("congestion"))
print("As of:", result.get("as_of"))
print("Recent average calls per day:",
metrics.get("recent_avg_portcalls_per_day"))
print("Baseline average calls per day:",
metrics.get("baseline_avg_portcalls_per_day"))
print("Ratio vs baseline:", metrics.get("ratio_vs_baseline"))

metadata = data.get("meta")
if isinstance(metadata, dict):
print("Source:", metadata.get("source"))
print("Reported lag:", metadata.get("data_lag_days"))
print("Methodology:", metadata.get("methodology"))

A missing field prints as None rather than being converted into a fabricated reading. For a production display, treat missing metrics or dates as unavailable, and decide explicitly whether to show an incomplete result. Do not substitute the deprecated congestion label for a missing ratio or infer a wait time from either field.

The example checks for an unsuccessful envelope, an empty result list, an HTTP failure, a connection failure, and invalid JSON. It does not assume a particular error-response schema. Consult the Documentation when implementing more detailed error handling.

Display and refresh the observation safely

Label the result as Shanghai port-call activity and display as_of next to the classification and ratio. Keep the averages labeled in port calls per day; the ratio itself has no units. A dashboard can show both the recent average and baseline so readers can understand what the ratio compares.

The sample’s as_of is a date, not a timestamp with a stated timezone. Preserve it as the returned observation date instead of inventing a time of day or timezone. If your application also records when it fetched the response, keep that retrieval time separate from as_of.

Cache the last successful response if your application needs repeated reads, and store the observation date with the cached values. Choose a refresh policy that reflects the source’s weekly republication and your own freshness requirements; frequent requests cannot turn a lagged series into live vessel positions. Guest users should also account for the 100-requests-per-day-per-IP limit when several readers share a service.

If a refresh fails, distinguish the cached observation from a newly retrieved one and continue showing its original as_of. If there is no usable result, show the data as unavailable rather than displaying a zero ratio or a guessed activity level. The sample contains a results list, but it does not establish a pagination contract; do not build pagination assumptions from this single response.

FAQ

Is this a live Shanghai vessel waiting-time API?

No. The endpoint reports lagged PortWatch port-call activity relative to a baseline. It does not measure vessel wait or anchorage time.

How recent is the Shanghai observation?

The typical lag is approximately 6–7 days, while the sample metadata reports a 4–11-day range. Read the result’s as_of date and returned lag information for the observation you receive.

Should I use activity or congestion?

Prefer activity for new integrations. The response methodology identifies congestion as a deprecated alias, and neither label should be presented as a measured vessel wait.

Can I call the endpoint without an API key?

Yes. Guest access without a key is limited to 100 requests per day per IP. For authenticated calls, use the X-API-Key header shown in the request.

Register to obtain an API key and integrate the Shanghai activity reading with its observation date and baseline context.