Get DataMotion in the Microsoft Azure Marketplace. Learn More.
Top Search Suggestions
Blog
Building a Secure and Compliant Message Center with React and DataMotion
Building a Secure and Compliant Message Center with React and DataMotion
Prerequisites Setting up the Project Setting Up the Backend with DataMotion APIs Initiate the server Installing Essential Libraries Setting up the Basic Server DataMotion API Integration in Server Fetching an Access Token: Fetching Emails: Verifying Backend Setup React Frontend: Fetching and Displaying Emails API Call with useEffect Displaying Emails Conclusion
git clone <url> cd DataMotion-SMC-starter-codenpm install
Markdown
Setting Up the Backend with DataMotion APIs
mkdir servercd server
Markdown
Initiate the server
npm init -y
Markdown
Installing Essential Libraries
express: A framework to build our server.
axios: To make HTTP requests.
cors: To enable Cross-Origin Resource Sharing.
dotenv: To manage environment variables from a .env file.
npm install express axios cors dotenv
Markdown
Setting up the Basic Server
touch server.js
Markdown
constexpress = require('express');constaxios = require('axios');constcors = require('cors');require('dotenv').config();constapp = express();constPORT = 5000;app.use(cors()); app.use(express.json());app.get('/', (req, res) => {res.send('Server is running!');});app.listen(PORT, () => {console.log(`Server is running on https://localhost:${PORT}`);});
JavaScript
node server.js
Markdown
DataMotion API Integration in Server
Fetching an Access Token:
app.get('/token', async (req, res) => {try {constresponse = awaitaxios.post('https://api.datamotion.com/SMC/Messaging/v3/token', {grant_type:"client_credentials",client_id:process.env.CLIENT_ID, // assuming you have named it CLIENT_ID in .envclient_secret:process.env.CLIENT_SECRET// assuming you have named it CLIENT_SECRET in .env });res.json(response.data); } catch (error) {res.status(500).json({ message:"Error fetching token", error:error.response.data }); }});
JavaScript
Fetching Emails:
app.get('/messages', async (req, res) => {try {// First, get the tokenconsttokenResponse = awaitaxios.post('https://api.datamotion.com/SMC/Messaging/v3/token', {grant_type:"client_credentials",client_id:process.env.CLIENT_ID, // assuming you have named it CLIENT_ID in .envclient_secret:process.env.CLIENT_SECRET// assuming you have named it CLIENT_SECRET in .env });// Use the token to get the message summariesconstmessagesResponse = awaitaxios.get('https://api.datamotion.com/SMC/Messaging/v3/content/messages/?folderId=1&pageSize=10&pageNumber=1&sortDirection=DESC&metadata=true', {headers: {Authorization:`Bearer ${tokenResponse.data.access_token}` } });res.json(messagesResponse.data); } catch (error) {res.status(500).json({ message:"Error fetching messages", error:error.response.data }); }});
JavaScript
Verifying Backend Setup
React Frontend: Fetching and Displaying Emails
npm start
Markdown
useState: A hook that gives a component its own data storage. It returns the data’s current value and a function to update it. When the data changes, the component re-renders.
useEffect: A hook that runs code after the component renders. We use it here to fetch emails once the component appears on screen.
API Call with useEffect
importReact, { useState, useEffect } from'react';import { ListGroup } from'react-bootstrap';constInbox = () => {const [emails, setEmails] = useState([]);useEffect(() => {// Call our server to get emailsfetch('https://localhost:5000/messages') .then(response=> {if (!response.ok) {thrownewError('Network response was not ok'); }returnresponse.json(); }) .then(data=> {// Sort emails by createTime from newest to oldestconstsortedEmails = data.items.sort((a, b) =>newDate(b.createTime) - newDate(a.createTime));setEmails(sortedEmails); }) .catch(error=> {console.error('Error fetching messages:', error); }); }, []);// Helper function to format the date or show the time if the email was received todayconstformatDate = (dateString) => {constdate = newDate(dateString);consttoday = newDate();if (date.toDateString() === today.toDateString()) {returndate.toLocaleTimeString(); }returndate.toLocaleDateString(); };return (// Rendering logic here... );};exportdefaultInbox;
JavaScript
Displaying Emails
// ... rest of the Inbox component ...return (<divclassName="col-md-9 pt-3"><ListGroupclassName='inbox-style'>{emails.map(email=> (<ListGroup.Itemkey={email.messageId}className='email-item'><div>{/* Email Address of Sender */}<pclassName="sender-email">{email.senderAddress}</p>{/* Email Subject */}<pclassName="email-subject">{email.subject}</p></div>{/* Email Received Date/Time */}<divclassName="date-received">{formatDate(email.createTime)}</div></ListGroup.Item> ))}</ListGroup></div> );};