#Example api wix-fetch wix-http-functions #backend #frontend #client #server
Demonstrates
-
Expose an API using wix-http-functions
-
Access (consume) an external API using wix-fetch
-
Web modules - server-side (backend) code
-
Calling Server-side Code from the Front-end with Web Modules
Required for this example
- A cold beer.
About the examples
MyApi (server/API app)
MyApi exposes an API by defining HTTP functions in the http-functions.js backend file. These functions are not called in code. Rather, they are invoked when an HTTP request, using the associated URL for that HTTP function, is received from a client app.
MyApi is essentially âpagelessâ. That is, the HOME page in this app is not needed for the API and only exists since a Wix site cannot exist without a page.
Important: You must save and publish your API site (MyApi) for changes to take effect on the testing and production endpoints.
MyApiClient (client app - consumes the API in MyApi)
MyApiClient uses the wix-fetch API to âtalkâ to the API exposed in MyApi. MyApiClient does not have any database collections of its own. Instead, it sends fetch requests to the API exposed in MyApi, which ultimately invoke the appropriate HTTP functions (endpoints) in the MyApi backend file http-functions.js .
Using the API rules as detailed in the wix-http-functions API , replace the string API_BASE_URL in the serviceModule.jsw file with the Base URL for the MyApi app.
MyApiClient uses a GET fetch to the API to retrieve a list of recipes which are displayed in a Repeater. Clicking on a recipe opens a Lightbox to display the recipe. The user can rate the recipe and a POST fetch request is sent to update the recipeâs rating in the Lightbox.
MyApiCombined (bonus app)
This is a bonus app which combines both of the above apps into one. Combining the API together with the consuming client makes debugging easier by allowing the display of console.log() diagnostic statements from both server and client to appear in the browserâs Javascript console. After the API and its interfacing have been debugged, you can duplicate the site and use one as the server app (API) and the other as the client app. Then just delete the unnecessary files from each app.
Using the API rules as detailed in the wix-http-functions API , replace the string API_BASE_URL in the serviceModule.jsw file with the Base URL for the MyApi app.
Important: You must save and publish your site (MyApi) for changes to take effect on the testing and production API endpoints.