web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Unanswered

400 Error when calling Events API from an Azure function

(0) ShareShare
ReportReport
Posted on by

I am trying to call the Events API from an Azure function using node.js

I have created the web application by following 

https://learn.microsoft.com/en-us/dynamics365/marketing/developer/register-web-application-events-api

I got the api endpoint and token.

I also registered my Azure function url as the origin when creating the web application.

For example if my function app url is

FnApp Url

https://msazfunctionapp.azurewebsites.net/api/msazfunctionname?code=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=

In the origin field I registered 

https://msazfunctionapp.azurewebsites.net/

After creating the web application , I followed below link to use Events API in an Azure function

https://learn.microsoft.com/en-us/dynamics365/marketing/developer/using-events-api

But when I run the function from azure portal using Code Test tab , I see the below error in response 

Response status : 400
2023-06-04T06:40:50Z [Information Error in Events Api : SyntaxError: Unexpected end of JSON input
Here is the node.js code I used in azurefunction to call the Events API
const https = require('https');





module.exports = async function (context, req) {



    try {

        context.log('JavaScript HTTP trigger function for D365 Events API processed a request.');



        

        

        try {

        const requestOptions = {

        method : 'GET',

        headers : { 'Content-Type': 'application/json' }

        };

     



        const fetchUrl = 'https://xxxxxxxxxxxxxxxxxxx.svc.dynamics.com/EvtMgmt/api/v2.0/events/published?emApplicationtoken=xxxxx-xxxxx-xxxxxxxxx’;

        const response = await fetch(fetchUrl);

        

        context.log('Response status : '    response.status);

        

        const respData = await response.json();

        const JSONrespData = JSON.stringify(respData);

        

        context.log('Response data as string : '   respData);

        context.log('Response data as Object : ' , respData);

        context.log('Response data JSON Stringified : '   JSONrespData);

        } catch(err) {

            context.log('Error in Events Api : '   err);

        }



       // context.res = data;

        const responseJSON = {

            "data" : respData

        }

        context.res.headers = { "Content-Type": "application/json" };

        context.res.headers = { "content-type": "application/json" };

        context.res.body = responseJSON;

        

        context.done();



        

    } catch(err) {

        context.res = {

            status: 500

        };

    }

}
The documentation to use Events API mentions that "If you do not provide a web application token or the Origin Header, the Events API will return HTTP 400 Bad Request."
I have provided the token but I don't think we can manually specify the Origin header

I have the same question (0)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Pallavi Phade Profile Picture

Pallavi Phade 98

#2
Tom_Gioielli Profile Picture

Tom_Gioielli 60 Super User 2025 Season 2

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 43 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans