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:
- Credentials never reach the client. API keys / OAuth tokens live in the resource config; the backend splices them onto every outbound request.
- 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.
- Upstream URL —
https://api.stripe.com,https://api.openai.com, or your own service. - Headers JSON —
{"Authorization": "Bearer sk_…", "X-Account": "..."}(any headers you want injected on every request). - Egress (Pro and Team) — leave on PyCorner-hosted for default behavior, or pick a tunnel agent for a fixed-IP / firewall-piercing route.
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.
- Passed under their own names:
Content-Type,Content-Language,Content-Disposition,Content-Range,Accept-Ranges,Cache-Control,Expires,ETag,Last-Modified,Age,Vary,Retry-After,Link,Allow,Deprecation,Sunset,Request-Id,X-Request-Id,X-Correlation-Id,X-Total-Count, and the rate-limit headers (RateLimit*,X-RateLimit-*,X-Rate-Limit-*). - Every other header arrives as
X-Upstream-<name>(X-Upstream-Stripe-Version,X-Upstream-WWW-Authenticate, …), a name browsers give no meaning to. In PythonResponse.headersshows it under its own name again, sor.headers["stripe-version"]works as before. In JavaScript readr.headers.get("x-upstream-stripe-version"). - Never passed:
Set-Cookie(an upstream cannot set cookies in your visitors’ browsers; keep a session on the upstream’s side or in your app), and what belongs to the connection (Content-LengthandContent-Encodingare the proxy’s own: the body arrives decoded). - Redirects: a redirect that stays below the resource’s upstream URL is followed through the proxy again, with the resource’s keys. One to another host is followed by the browser directly, without the keys, as a request any page could make. The upstream’s own
Locationis inX-Upstream-Location.
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:
- The upstream allowlists by IP and you don’t want to share PyCorner’s IP with every other tenant.
- The upstream is internal to your network (
http://jira.corp:8080,http://192.168.1.42:8200, etc.). - You’d rather no API keys ever leave your infrastructure.
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
- Bodies are streamed as base64 between the backend and the agent (Mode 2 + agent egress) — fine for typical API payloads, not great for hundreds of MB.
- The agent egress path uses
urllib.requestserver-side, which handles HTTPS / redirects / common content types without further dependencies. - The default timeout is 30 seconds (configurable per-resource via
timeoutin the config). The backend adds a few seconds of slack to the wait so the agent has time to surface its own timeout cleanly.
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.