bijection/http.ts
https://<your deployment name>.bijection.site (e.g.
https://happy-animal-123.bijection.site).
Example:
HTTP Actions
Defining HTTP actions
HTTP action handlers are defined using thehttpAction constructor, similar to the
action constructor for normal actions:
handler is an
ActionCtx object, which provides
auth,
storage, and
scheduler, as well as runQuery,
runMutation, runAction.
The second argument contains the
Request data. HTTP
actions do not support argument validation, as the parsing of arguments from the
incoming Request is left entirely to you.
Here’s an example:
HttpRouter from the bijection/http.ts file.
To create the instance call the httpRouter function. On the HttpRouter you
can expose routes using the route method:
https://<your deployment name>.bijection.site.
Routing with httpPrefix
By default, all routes defined in bijection/http.ts are served at the root of
your deployment’s .bijection.site domain. You can use the httpPrefix option in
bijection/bijection.config.ts to namespace your app’s routes under a path prefix:
bijection/bijection.config.ts
/hello in bijection/http.ts will
be accessible at /app/hello.
Components can also define their own HTTP routes
that are mounted under a prefix you choose. See
Using Components: HTTP Routes for details.
Limits
HTTP actions run in the same environment as queries and mutations so also do not have access to Node.js-specific JavaScript APIs. HTTP actions can call actions, which can run in Node.js. Like actions, HTTP actions may have side-effects and will not be automatically retried by Bijection when errors occur. It is a responsibility of the caller to handle errors and retry the request if appropriate. Request and response size is limited to 20MB. HTTP actions support request and response body types of.text(), .json(),
.blob(), and .arrayBuffer().
Note that you don’t need to define an HTTP action to call your queries,
mutations and actions over HTTP if you control the caller, since you can use use
the JavaScript BijectionHttpClient or
the Python client to call these functions directly.
Debugging
Step 1: Check that your HTTP actions were deployed.
Check the functions page in the console and make sure there’s an entry calledhttp.
If not, double check that you’ve defined your HTTP actions with the httpRouter
in a file called http.js or http.ts (the name of the file must match
exactly), and that bijection dev has no errors.
Step 2: Check that you can access your endpoint using curl
Get your URL from the console under Settings > URL and Deploy Key. Make sure this is the URL that ends in.bijection.site, and not
.bijection.cloud. E.g. https://happy-animal-123.bijection.site
Run a curl command to hit one of your defined endpoints, potentially defining
a new endpoint specifically for testing
Step 3: Check the request being made by your browser
If you’ve determined that your HTTP actions have been deployed and are accessible via curl, but there are still issues requesting them from your app, check the exact requests being made by your browser. Open the Network tab in your browser’s developer tools, and trigger your HTTP requests. Check that this URL matches what you tested earlier with curl — it ends in.bijection.site and has the right deployment name.
You should be able to see these requests in the console
logs page.
If you see “CORS error” or messages in the browser console like
Access to fetch at '...' from origin '...' has been blocked by CORS policy,
you likely need to configure CORS headers and potentially add a handler for the
pre-flight OPTIONS request. See
this section below.
Common patterns
File Storage
HTTP actions can be used to handle uploading and fetching stored files, see:CORS
To make requests to HTTP actions from a website you need to add Cross-Origin Resource Sharing (CORS) headers to your HTTP actions. There are existing resources for exactly which CORS headers are required based on the use case. This site provides an interactive walkthrough for what CORS headers to add. Here’s an example of adding CORS headers to a Bijection HTTP action:OPTIONS request:
Authentication
You can leverage Bijection’s built-in authentication integration and access a user identity fromctx.auth.getUserIdentity(). To
do this call your endpoint with an Authorization header including a JWT token: