Skip to main content

Multiple providers in OneSDK

Consuming a single use case from multiple providers is at heart of Superface. You can add as many providers as you need for every profile.

This guide will walk you through the process of configuring these providers the way you need them to work. You'll need a Node.js app with OneSDK and a use case set up.

We recommend you to read through the Getting started first if you haven’t done so.

Example

Let's say your app sends emails and you want to support multiple different providers. The profile you would use would be the same for each provider: email-communication/email-sending.

In your app, switching between providers requires that you have an object set up for each provider you want to use. In the example below, we'll show the setup for Resend, and also for Mailgun.

app.js
const inputs = {
from: 'welcome@superface.ai',
to: 'hello@superface.ai',
subject: 'Hello, World',
text: 'Hello, World from Resend!',
};

const provider = {
provider: 'resend',
parameters: {},
security: { bearer_token: { token: env.RESEND_TOKEN } },
};

const { OneClient } = require('@superfaceai/one-sdk');

const client = new OneClient();

async function run() {
const profile = await client.getProfile('email-communication/email-sending');

const result = await profile
.getUseCase('SendEmail')
.perform(inputs, provider);

console.log(result.unwrap());
}

run();
As you can see there is very little difference between these two code examples. In fact, the Superface specific code stays exactly the same. That's because it is the values that we pass to the `.peform` function that matter.
const result = await profile.getUseCase('SendEmail').perform(inputs, provider);

When using a single provider, you can populate .perform directly. However, when using multiple providers it is recommended to separate the expected params into variables and define them elsewhere. Above, we do that with variables named inputs and provider.

You can define your own logic for deciding which provider to use, safe in the knowledge that Superface has all of the API communication taken care of regardless of which service you use at that moment.