APIs

Reach any HTTP API from your Pyodide app — with secret keys living server-side and CORS dealt with.

API resources let your in-browser app make HTTP calls to any URL through the PyCorner backend (or, on Pro and Team, through your own tunnel agent). The two big wins:

  1. Credentials never reach the client. API keys / OAuth tokens live in the resource config; the backend splices them onto every outbound request.
  2. No CORS theatre. Your app calls a same-origin PyCorner URL; the backend talks to the upstream from outside the browser, where CORS doesn’t apply.

Create a resource

Resources → APIs → New API.

Use it from your app

import pycorner
import streamlit as st

stripe = pycorner.api("stripe")

charges = stripe.get("/v1/charges", params={"limit": 5}).json()
st.write(charges["data"])

refund = stripe.post(
    "/v1/refunds",
    json={"charge": "ch_abc123", "reason": "requested_by_customer"},
)
st.write(refund.json())

The returned Response has .status_code, .headers, .content, .text, .json(), and an .ok boolean (200 ≤ status_code < 300). Status passthrough is verbatim — if upstream returns 418 you’ll see 418 in your client.

Response headers

The upstream’s answer is served from the address your app runs on, which it shares with every other app, so its headers do not reach the browser as they come: a header is an instruction to the browser about that address.

A service worker’s script cannot be loaded through the proxy.

Methods

api.get(), api.post(), api.put(), api.patch(), api.delete() are all available. Or use the lower-level form:

api.request("PATCH", "/v1/things/42", json={"name": "renamed"})

params= becomes query string, json= becomes a JSON body with Content-Type: application/json, data= is a string-or-bytes body with application/octet-stream, headers= are merged on top of the configured upstream_headers.

Agent egress (Pro and Team)

The default route is PyCorner-hosted: our backend makes the upstream call from our server IP. For most apps that’s enough — but it doesn’t help if:

Picking a tunnel agent in the Egress dropdown solves all three. The flow becomes:

Stlite → /api/v1/data/x/api/<name>/<path>
       → PyCorner backend resolves resource, sees agent_id
       → backend sends an "http_request" message over the tunnel WS
       → your agent on the customer's network performs the upstream call
       → response goes back through the same path in reverse

The agent’s egress IP is whatever the customer’s network’s egress IP is. Their firewall sees a normal outbound HTTP request from a known host. Their internal APIs are reachable because the agent is inside the network.

A quick way to verify it’s working:

import pycorner
import streamlit as st

agent_route  = pycorner.api("upstream-via-agent")
hosted_route = pycorner.api("upstream-direct")

st.subheader("Via agent")
st.json(agent_route.get("/get").json())   # `origin` will be your office IP

st.subheader("Hosted")
st.json(hosted_route.get("/get").json())  # `origin` will be PyCorner's

Limits

Method allowlist (optional)

You can restrict which HTTP verbs a resource accepts by setting allowed_methods in the resource config (via Django admin or API). Useful if you want to expose a read-only view of an upstream:

{
  "upstream_url": "https://api.example.com",
  "upstream_headers": {"X-API-Key": "..."},
  "allowed_methods": ["GET"]
}

POST/PUT/DELETE from the client will get a 405 from the backend without the upstream ever being called.