Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us at the 2025 Microsoft Fabric Community Conference. March 31 - April 2, Las Vegas, Nevada. Use code FABINSIDER for $400 discount. Register now

Reply
v-rajethakur
Microsoft Employee
Microsoft Employee

How to read parameters in MS fabric notebook, passed as request body from an API call

I am running a microsoft fabric notebook on demand from my c# code , via fabcric POST API call. My note book requires 3 parameters that I need to pass as a Json body to this POST API. Below is the sample request.

{

   "FirstName":"John",

   "LastName":"Smith",

    "LoginDate":"2/7/2025"

}

 

Now in notebook cell, how can I get all these 3 fields and print it. Looking for something like below - 

 

vrajethakur_0-1738972348554.png

 

 

12 REPLIES 12
v-ssriganesh
Community Support
Community Support

Hi @v-rajethakur,
Thank you for posting your quey in Microsoft fabric community forum.

I wanted to share a solution for reading parameters from the request body in a Microsoft Fabric notebook. I reproduced the scenario you described and used the following code to successfully extract and print the parameters:

Code:

import json 

json_body = ''' 

   "FirstName":"John", 

   "LastName":"Smith", 

   "LoginDate":"2/7/2025" 

''' 

 

params = json.loads(json_body) 

 

fName = params.get('FirstName') 

lName = params.get('LastName') 

date = params.get('LoginDate') 

 

print(f"First Name: {fName}") 

print(f"Last Name: {lName}") 

print(f"Login Date: {date}") 

Could you please confirm if this matches your expected output? Kindly refer to the screenshot below:

vssriganesh_0-1739171667080.png

This approach simulates receiving a JSON body from an API request. Remember to replace the simulated json_body with the actual method you use to read the request body in your notebook.

 

Please do not hesitate to reach out if you have any further questions. If this information is helpful, kindly accept it as a solution and consider giving a "Kudos" so other members can easily find it.
Thank you.

Hi @v-ssriganesh , many thanks for your response.  I am unable to get desired output with this approach. Could you please provide more info on how to read the request body. Below is the screenshot - from my notebook. And I think the second cell of defining request_body is not correct. How should I read the actual input coming from request body. 

 

vrajethakur_0-1739175135513.png

My sample request body is - 

{
"FirstName": "John",
"LastName": "Smith",
"LoginDate": "1/1/2021"
}

 

I can change  my request body as well if it helps in getting the desired output.

Hi @v-rajethakur,

Thanks for your response and the screenshot! It looks like the issue might be related to how the request body is captured in your notebook. To read the actual request body in your Microsoft Fabric notebook, you should use the parameters feature instead of defining a variable manually.

Here’s a revised approach that demonstrates how to correctly capture and use the parameters passed from your C# API call:

Updated Code:

import json 

params = parameters 

fName = params.get('FirstName') 

lName = params.get('LastName') 

loginDate = params.get('LoginDate') 

print(f"First Name: {fName}") 

print(f"Last Name: {lName}") 

print(f"Login Date: {loginDate}") 

  • Instead of using a variable like json_body, directly use parameters to read the incoming request body. This variable is automatically set when the notebook is invoked.
  • After implementing the above code, be sure to run your notebook again and check if the expected output shows up. If you need to alter the request body, ensure it matches the expected JSON format you're trying to read in the notebook.

If this helps, then please Accept it as a solution and dropping a "Kudos" so other members can find it more easily.
Hope this works for you!
Thank you.

Hi @v-ssriganesh , thanks for your answer. Now I am getting below error. Its unable to identify that parameters keyword. Is this an inbuild keyword ? After making the API call when I go and see the run history, I found below error -

 

vrajethakur_0-1739207247360.png

 

Hi @v-rajethakur,

Thanks for your response and for sharing the error details. I understand the issue with parameters is not a built-in keyword in Fabric Notebooks. Instead, to correctly retrieve parameters passed from an API call, you should use mssparkutils.env.get().

Please modify your notebook to retrieve parameters dynamically:

 

CopyEdit

import mssparkutils

fName = mssparkutils.env.get("FirstName")

lName = mssparkutils.env.get("LastName")

loginDate = mssparkutils.env.get("LoginDate")

 

print(f"First Name: {fName}")

print(f"Last Name: {lName}")

print(f"Login Date: {loginDate}")

 

  • Update your notebook to use mssparkutils.env.get().
  • Please ensure your API request includes parameters in the jobParameters section.
  • Re-run the notebook and check if the output is as expected.

Let me know if you need further clarification! If this solution works, please mark it as Accepted and consider giving a Kudos so other community members can find it easily.

Thank you.

Hi @v-ssriganesh , Now it id giving me this error - 

vrajethakur_0-1739209848385.png

 

Am I doing anything wrong here ?

Hi @v-rajethakur,

Thanks for your patience! I see that you are getting an AttributeError while trying to retrieve the parameters. This happens because mssparkutils.env.get() does not exist in Fabric Notebooks. My apologies for the confusion.

In Microsoft Fabric Notebooks, you should use mssparkutils.notebook.run() with widgets to pass and retrieve parameters. Here’s the correct way to do it:

Please ensure your API request sends parameters correctly using jobParameters:

json

CopyEdit

{

  "jobParameters": [

    {

      "name": "FirstName",

      "value": "John"

    },

    {

      "name": "LastName",

      "value": "Smith"

    },

    {

      "name": "LoginDate",

      "value": "1/1/2021"

    }

  ]

}

Modify your notebook code as follows:

python

CopyEdit

import mssparkutils

fName = mssparkutils.notebook.getArgument("FirstName", "Default_FirstName")

lName = mssparkutils.notebook.getArgument("LastName", "Default_LastName")

loginDate = mssparkutils.notebook.getArgument("LoginDate", "Default_LoginDate")

 

print(f"First Name: {fName}")

print(f"Last Name: {lName}")

print(f"Login Date: {loginDate}")                          

  • Update your notebook with the above code.
  • Ensure your API request body contains parameters inside jobParameters.
  • Run the notebook and check the output.

If this helps, then please Accept it as a solution and dropping a "Kudos" so other members can find it more easily.
Hope this works for you!
Thank you.

@v-ssriganesh , I got this error now - 

vrajethakur_0-1739213810649.png

 

Hi @v-rajethakur,

We regret the inconvenience caused and acknowledges your requirements. Please consider raising a Microsoft support ticket. You can create a Microsoft support ticket using the link below:
https://learn.microsoft.com/en-us/power-bi/support/create-support-ticket

If this helps, then please Accept it as a solution and dropping a "Kudos" so other members can find it more easily.

Thank you.

Hi @v-rajethakur,
Could you please confirm if the issue has been resolved after raising a support case? If a solution has been found, it would be greatly appreciated if you could share your insights with the community. This would be helpful for other members who may encounter similar issues.

Thank you for your understanding and assistance.

v-rajethakur
Microsoft Employee
Microsoft Employee

Thanks for the response @nilendraFabric , I Tried the same approach but got Default values in the reponse, not the actual one passed from API request.

 

This is my notebook code part - 

vrajethakur_0-1738990391339.png

 

{
"FirstName": "John",
"LastName": "Smith",
"LoginDate": "2/7/2025"
}

 

And here is the output I got after passing above mentioned request body through POST API -

 

vrajethakur_1-1738990507551.png

 

For triggering notebook I am using  Job Scheduler - Run On Demand Item Job through learn.microsoft.com.

 

vrajethakur_3-1738990809465.png

 

 

 

nilendraFabric
Community Champion
Community Champion

Hello @v-rajethakur 

 

At the very start of your notebook, add a cell that defines your parameters with default values. Then, mark this cell as a parameters cell so that Microsoft Fabric automatically replaces the defaults with the values passed via your API call

IMG_7151.png

 

# Parameters cell (toggle this cell as a parameter cell)
FirstName = "DefaultFirstName"
LastName = "DefaultLastName"
LoginDate = "DefaultLoginDate"

When your API call sends a JSON payload like this:

{
"FirstName": "John",
"LastName": "Smith",
"LoginDate": "2/7/2025"
}

Fabric will override the default values with the ones provided.

In next cell

 

print(f"First Name: {FirstName}")
print(f"Last Name: {LastName}")
print(f"Login Date: {LoginDate}")

 

Hope this helps.

 

Please accept the answer if this is helpful and give kudos

 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebFBC_Carousel

Fabric Monthly Update - February 2025

Check out the February 2025 Fabric update to learn about new features.

Feb2025 NL Carousel

Fabric Community Update - February 2025

Find out what's new and trending in the Fabric community.