This example showcases how you can use http outcalls to call a 3rd party provider to send email.Courier. Granting courier access to one of your email providers (eg gmail) it can send emails on our behalf via a http post request to their api.
Due to the implementation of https outcalls sending a request from each replica, idempotency was key to not sending duplicate emails. Courier provides an ability to pass an idempotency key. Courier can connect to one of your email providers and send an email on your behalf.
This example includes four containers:
- A frontend container containing a react application used to register api key and a form to send an email request to courier.
- Along side the front end canister an instance of the internet identity container is provided to authenticate against.
- A backend Motoko canister is included to handle registering an api key against a user, and calling the courier api.
- Another backend canister to demonstrate canister to canister calls
After authenticating with internet identity using easy-to-use library (agent-js), requests sent to the backend container can access the calling principal. Calling the registerKey function passing the courier api key will extract the principal id and store it in a hashmap userKeyMap with the key userId => courierApiKey.
It uses this library to generate a uuid to be used as an idempotency key sent to courier.
Version 12 is required for http outcalls
DFX_VERSION=0.12.0-beta.3 sh -ci "$(curl -fsSL https://internetcomputer.org/install.sh)"
Vessel is a package manager for ic canisters. Follow the instructions here to install the vessel binary.
Sign up with courier and connect one of your email providers.
If you want to test your project locally, you can use the following commands:
# Deploys your canisters to the replica and generates your candid interface
./scripts/deploy.sh localOnce the job completes, your application will be available at http://localhost:8000?canisterId={asset_canister_id}. There will be a link in the terminal to open the frontend canister.
- Press the
open with internet identitybutton on the home page. - It should open a new tab pointing to
http://localhost:8000/?canisterId={ii_canister_id}#authorize. This is the local II container we spun up. - Create a new anchor.
- Authorize app with this new anchor id
- Set the courier api key you fetched from here
- Send an email to a test address with a recipient, subject and body.
Additionally, if you are making frontend changes, you can start a development server with
npm startWhich will start a server at http://localhost:8080, proxying API requests to the replica at port 8000.
This example includes a canister that demonstrates canister to canister calls.
- Register the api key with:
dfx canister call canister_to_canister_backend registerKey '("key")'
It will the identity currently set in your local dfc installation
Check with
dfx identity whoami
If you want to try with a different user you can create a new identity
dfx identity create email_user
Set dfx to use this new user
dfx identity use email_user
- Send an email
dfx canister call canister_to_canister_backend sendEmail '("test@test.com", "cool subject", "this is the body")'