CORS
Call the Comecero API straight from a browser by adding your site's origin to the allowed CORS origins, and learn which origins are allowed without setup.
A page in a browser can call the API directly, for example to build a cart with a limited token. For that to work, the page's origin must be on your account's list of allowed origins. This page shows how to add yours.
Why it is needed
Browsers block a page on one site from reading responses from another site unless that site agrees. This is called cross-origin resource sharing (CORS). The API agrees only for origins you list, so a page on someone else's site cannot use your shoppers' tokens.
A call from a page whose origin is not allowed is refused with 403:
{
"error": {
"type": "forbidden",
"code": "invalid_origin",
"message": "Invalid Origin. The request has been sent from an origin that is not approved for CORS requests. Allowed origins for cross-origin requests can be configured through account settings.",
"reference": "c8kTbVL",
"status": 403,
"meta": null
}
}Add your origins
Add each site that calls the API from the browser under Settings > Technical in the dashboard, or with Update technical settings:
curl -X POST "https://api.comecero.com/api/v1/settings/technical" \
-H "Authorization: Bearer $COMECERO_TEST_TOKEN" \
-H "Content-Type: application/json" \
-d '{ "cors_allowed_origins": ["https://www.example.com", "https://shop.example.com"] }'- An origin is the scheme and host only, such as
https://www.example.com, with no path and no trailing slash. A port is not part of the match. - Live mode needs
https. Test mode also allowshttp. - You can list up to 15 origins. The list replaces the one you had, so send all of them.
- Test and live settings are separate. An origin listed in either one is allowed.
Allowed without setup
localhostand127.0.0.1, on any port, so you can build on your own machine.- Your shop's own host names (
app_hostsin the technical settings), such asexample.apps.comecero.com. - The Comecero dashboard.
Calls from a server or a tool like curl send no browser origin, so CORS does not apply to them.
What the API allows
For an allowed origin, the API answers the browser's preflight request and allows:
- Methods
GET,POST,PUT,PATCH,DELETEandOPTIONS. - Headers
Authorization,Content-Type,Accept,Origin,X-Requested-WithandCache-Control. - Reading the
X-Token-Expires-In-Secondsresponse header, which tells the page how long its token has left.
Browsers keep the preflight answer for up to 24 hours, so most calls do not pay for an extra round trip.
CORS is for limited tokens and for trusted tokens in staff tools. Never send a secret token from a browser, even from an allowed origin. See Authentication.
Some accounts may not create payments or save payment methods from a browser on another origin. If a payment call from your page gets 403 with code invalid_origin while other calls work, contact support.
Next steps
API versioning
How the Comecero API is versioned, which changes can arrive without notice in version 1, and how to write code that keeps working as the API grows.
License services
Give buyers a license key after each sale, either from a list of keys you upload or from your own license server that Comecero calls with the order.