Communicating with Motoko canister from different domain?

My app is storing data in a motoko canister. This works fine when my app is hosted on the internet computer.
Is it also possible to communicate with the canister when the app is running on a different domain? I implemented Login with Internet Identity, this works fine on non-IC domains. But now I want to retreive the data for that user, but it’s not working.

I added my canister id as host to the agentOptions when creating the storage actor. But there are two problems:

  • I do see request to this canister when retreiving data, but it doesn’t get any data.
  • When I store data, this host url is ignored and it submits data to the url from where the app is loaded

This is the code creating my actor:

async function handleAuthenticated(authClient: AuthClient) {
  const identity = await authClient.getIdentity();
  storage_actor = createActor(canisterId as string, {
    agentOptions: {
      host: 'https://my-canister-id.ic0.app',
      identity,
    },
  });
}

Any suggestions? Is it possible what I’m trying to do?

1 Like

I also have to set the host to access my canister because I am calling it from web workers. So not exactly the same reason but, same goal.

I don’t use the pre-build createActor function but, refactored its code to make the optional host more obvious.

So, if it can help, my generic function to create actor :point_right: deckdeckgo/actor.utils.ts at main · deckgo/deckdeckgo · GitHub

Note: as host value, as I am hosting it on the IC, I path ${window.location} to my worker, so https://.....raw.ic0.app. Not sure then if using raw also helps?

I think there is nothing Motoko-specific in this question, as it is about the JS front end, unless I am mistaken. I suggest to retitle so that more people might look at this.

As host I suggest to use simply https://ic0.app. there is little reason to include the canister id in the URL that’s used by the agent, as it is ignored (and might stop working in the future). You can talk to any canister through this entry point.

1 Like

Yes, setting host to https://ic0.app if you are making calls to the IC from a different domain is correct

Thanks, this has been fixed!

Setting the host to https://ic0.app works.
I also had an error in my code, I had two instances of my actor, one for requests where the user was authenticated with an Internet Identity and one normal unauthenticated actor. I didn’t initialize the unauthenticated actor with the host.

1 Like