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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Dataverse custom API c...
Power Apps
Unanswered

Dataverse custom API call from power app code (react)

(0) ShareShare
ReportReport
Posted on by 21
Good morning,
 
I'm developing a power apps code app using react in VS code (PCF). This app is used in the power app context and connected to a dataverse DB.
 
For the CRUD dataverse calls, I use the services classes provided with PCF. The security is natively handled for the power platform connected user.
 
I made a custom API with a plugin to make some heavy work on my dataverse tables.
 
Now I want to call the custom API in my react app but I don't know how to pass the current connected user (from the power app context) ?
I receive an unauthorized response with the code below.
    const apiUrl = `${domainUrl}/api/data/v9.2/NaloDuplicateMasterplanAPI`;
    const response = await fetch(apiUrl, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(requestBody),
    });
    if (!response.ok) {
      throw new Error(`Erreur HTTP: ${response.status}`);
    }
 
I also tried with the DataClient.executeAsync function in the CRUD client but without luck, I don't know how to make the execute call for an API (and if it's possible) 
executeAsync: <TRequest, TResult>(operation: IDataOperation<TRequest>)
 
Thanks a lot in advance !
Fabrice
I have the same question (0)
  • Assisted by AI
    Ram Prakash Duraisamy Profile Picture
    5,877 Super User 2026 Season 1 on at
     
    Can you give a try for AI assisted code ?
     
    import { IInputs, IOutputs } from "./generated/ManifestTypes";
    
    export class MyControl implements ComponentFramework.StandardControl<IInputs, IOutputs> {
    
        private context: ComponentFramework.Context<IInputs>;
    
        public init(
            context: ComponentFramework.Context<IInputs>,
            notifyOutputChanged: () => void,
            state: ComponentFramework.Dictionary,
            container: HTMLDivElement
        ): void {
            this.context = context;
        }
    
        private async callCustomAPI() {
    
            const userId = this.context.userSettings.userId.replace(/[{}]/g, "");
    
            const request = {
                UserId: userId, // parameter defined in your Custom API
    
                getMetadata: () => ({
                    boundParameter: null,
                    operationType: 0, // 0 = Action, 1 = Function
                    operationName: "new_MyCustomAPI", // your Custom API logical name
                    parameterTypes: {
                        "UserId": {
                            typeName: "Edm.String",
                            structuralProperty: 1 // PrimitiveType
                        }
                    }
                })
            };
    
            try {
                const response = await this.context.webAPI.execute(request);
    
                if (response.ok) {
                    const result = await response.json();
                    console.log("API Result:", result);
                }
            } catch (error) {
                console.error("Custom API Error:", error);
            }
        }
    
        public updateView(context: ComponentFramework.Context<IInputs>): void {}
    
        public getOutputs(): IOutputs { return {}; }
    
        public destroy(): void {}
    }
    

    Please mark as answer if my suggestion helps.
    Subscribe here for More Useful videos : https://www.youtube.com/@rampprakash3991
     
  • FN-30011324-0 Profile Picture
    21 on at
    Hi Ram Prakash Duraisamy,

    Thanks for your answer.
     
    Unfortunately, I already tried a lot of AI answers but without any luck. I don't have access to context.webAPI or Xrm in a code app (CLI).

    I finally found a solution : I created a custom connector linked to my custom API and I was able to pac code add-data-source the connector to my react project. This created a service class to call the connector. This also requires some config in the azure app registration but at the end, it works.
     
    King regards
    Fabrice

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
Valantis Profile Picture

Valantis 424

#2
WarrenBelz Profile Picture

WarrenBelz 355 Most Valuable Professional

#3
11manish Profile Picture

11manish 290

Last 30 days Overall leaderboard