Replace Services with npm Modules
Overview
You should migrate deprecated third-party service integrations to use the corresponding official libraries available from npm. The sections later on this page contain details and examples for each service.
To migrate from a deprecated service:
Add a library for the service from
npm
to your app.Import the library in your functions. Replace any calls to the built-in service actions with calls to the corresponding methods in the imported library.
If the service has webhooks, convert them to HTTPS endpoints.
HTTP Service
Replace HTTP requests sent through an HTTP Service client with calls to an HTTP library like node-fetch or axios.
node-fetch
npm i node-fetch@^2
Important
Atlas App Services does not support v3 of node-fetch
. Use v2 instead.
exports = async function() { const http = context.services.get("myHttp"); const response = await http.get({ url: "https://www.example.com", headers: { "Content-Type": [ "application/json" ] } }) return response.body.text() }
exports = async function() { const fetch = require("node-fetch"); // require calls must be in exports function const response = await fetch("https://www.example.com", { method: "GET", headers: { "Content-Type": "application/json" } }) return response.text() }
axios
npm i axios
exports = async function() { const http = context.services.get("myHttp"); const response = await http.get({ url: "https://www.example.com", headers: { "Content-Type": [ "application/json" ] } }) return response.body.text() }
exports = async function() { const axios = require("axios"); // require calls must be in exports function const response = await axios.get("https://www.example.com", { headers: { "Content-Type": "application/json" } }) return response.data }
Twilio Service
Replace calls through a Twilio Service client with calls to the official Twilio Node Helper Library.
npm i twilio
To authenticate Twilio requests, store your Account SID and Auth Token as values. You can then access them within functions and pass them to the SDK.
exports = async function() { const twilio = context.services.get("myTwilio"); twilio.send({ to: "+15558675309", from: "+15551234567", body: "Hello from App Services!", }); }
exports = async function() { // Note: require calls must be in the exported function const twilio = require('twilio')( // Your Account SID and Auth Token from https://www.twilio.com/console // Specify the same Twilio credentials as the service configuration context.values.get("TwilioAccountSID"), context.values.get("TwilioAuthToken"), ) await twilio.messages.create({ to: "+15558675309", from: "+15551234567", body: "Hello from App Services!", }) }
AWS Service
Replace calls through an AWS Service client with calls to the official AWS JavaScript SDK.
npm i aws-sdk
Important
App Services does not yet support version 3 of the AWS SDK. Use the version 2 SDK to replace the deprecated AWS Service in your functions.
To authenticate AWS requests, store your Access Key ID and Secret Access Key as values. You can then access them within functions and pass them to the SDK.
S3
exports = async function() { const s3 = context.services.get("myAWS").s3("us-east-1"); const putResult = await s3.PutObject({ Bucket: "bucketName", Key: "keyName", Body: EJSON.stringify({ hello: "world" }), }); const getResult = await s3.GetObject({ Bucket: "bucketName", Key: "keyName", }); }
exports = async function() { const S3 = require('aws-sdk/clients/s3'); // require calls must be in exports function const s3 = new S3({ accessKeyId: context.values.get("awsAccessKeyId"), secretAccessKey: context.values.get("awsSecretAccessKey"), region: "us-east-1", }); // https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property const putResult = await s3.putObject({ Bucket: "bucketName", Key: "keyName", Body: EJSON.stringify({ hello: "world" }), }).promise(); // https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getObject-property const getResult = await s3.getObject({ Bucket: "bucketName", Key: "keyName", }).promise(); }
Kinesis
exports = async function() { const kinesis = context.services.get("myAWS").kinesis("us-east-1"); const putResult = await kinesis.PutRecord({ Data: EJSON.stringify({ hello: "world" }), StreamName: "myStream", PartitionKey: "myPartition", }); }
exports = async function() { const Kinesis = require('aws-sdk/clients/kinesis'); // require calls must be in exports function const kinesis = new Kinesis({ accessKeyId: context.values.get("awsAccessKeyId"), secretAccessKey: context.values.get("awsSecretAccessKey"), region: "us-east-1", }); // https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Kinesis.html#putRecord-property const putResult = await kinesis.putRecord({ Data: EJSON.stringify({ hello: "world" }), StreamName: "myStream", PartitionKey: "myPartition", }).promise(); }
Lambda
exports = async function() { const lambda = context.services.get('MyAwsService').lambda("us-east-1"); const invokeResult = await lambda.Invoke({ FunctionName: "myLambdaFunction", Payload: EJSON.stringify({ hello: "world" }), }); };
exports = async function() { const Lambda = require('aws-sdk/clients/lambda'); // require calls must be in exports function const lambda = new Lambda({ accessKeyId: context.values.get("awsAccessKeyId"), secretAccessKey: context.values.get("awsSecretAccessKey"), region: "us-east-1", }); // https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html#invoke-property const invokeResult = await lambda.invoke({ FunctionName: "myLambdaFunction", Payload: EJSON.stringify({ hello: "world" }), }).promise(); }
SES
exports = async function() { const ses = context.services.get('MyAwsService').ses("us-east-1"); const sendResult = await ses.SendEmail({ Source: "sender@example.com", Destination: { ToAddresses: ["receiver@example.com"] }, Message: { Body: { Html: { Charset: "UTF-8", Data: `This is a message from user ${context.user.id} sent through AWS SES` } }, Subject: { Charset: "UTF-8", Data: "Test Email Please Ignore" }, }, }); };
exports = async function() { const SES = require('aws-sdk/clients/ses'); // require calls must be in exports function const ses = new SES({ accessKeyId: context.values.get("awsAccessKeyId"), secretAccessKey: context.values.get("awsSecretAccessKey"), region: "us-east-1", }); // https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SES.html#sendEmail-property const sendResult = await ses.sendEmail({ Source: "sender@example.com", Destination: { ToAddresses: ["receiver@example.com"] }, Message: { Body: { Html: { Charset: "UTF-8", Data: `This is a message from user ${context.user.id} sent through AWS SES` } }, Subject: { Charset: "UTF-8", Data: "Test Email Please Ignore" }, }, }).promise(); }