2sxc Web API
2sxc has a fully featured WebApi for use in JavaScript or as Headless CMS. So you can call HTTP GET
to an endpoint like app/News/data/NewsItems/
and get a list of news items.
You can also create / modify data using REST, access Queries and work with your custom Web-APIs - across all 2sxc platforms (Dnn / Oqtane / Custom) and with the security you need.
Simple Example
Here's an example JavaScript which would run in a 2sxc News-App on a Dnn page:
// Get news as a promise
var allNewsPromise = sxc.webApi.get('app/auto/data/News/');
// now log to console
allNewsPromise.then(data => console.log(data));
This example has some magic happening in the background. Specifically 3 important things happen on the client before sending:
- The real URL which is requested is a bit longer, but the
webApi
takes care of that. - The server needs to know what App and Module the script is calling from. This Context information is also added by the
webApi
. - To add security, a crypto-token is added which will prove that the JavaScript requesting this is running on the page. This is also added by
webApi
.
When the server receives the request, it too does important stuff.
- First it will use the URL and Context information to figure out what Site, Language and App should be accessed.
- The server will do some security checks to see if this request should be answered.
- Then it returns the data in a simple JSON format
Get Started Step 1: Decide which Endpoint you need
This checklist should help you decide:
Getting Started
Your 1-2-3 steps for using data endpoints are:
- Decide which endpoint you need
- Create / Publish the endpoint
- For standard endpoints, set the permissions as you need them
- For custom endpoints, write the C# code
- Write the code to read the data (typically in JavaScript)
Conventions Used
- REST means that there is a url convention to access data like
.../blogposts/25
- REST also specifies that an HTTP GET is for reading, HTTP POST is for writing, etc.
- All the APIs use JSON for sending/receiving data. This is the default schema.
Demo App and further links
You should find some code examples in this demo App
Recommended Reading
History
- Introduced Content-REST API in 2sxc 5.x
- Query added in 2sxc 8.10
- Enhanced with Polymorph Editions in 2sxc 9.35 (allowing subfolder/api)