How to Translate English to Mandalorian with Twilio Serverless and SMS
Tags: serverless, javascript, twilio, tutorial
Published: 2021-05-03 20:09:14

---
title: How to Translate English to Mandalorian with Twilio Serverless and SMS
published: true
description: Text a phrase to +14807573107 to translate it into Mandalorian and learn how to build the app with Twilio Serverless and SMS in JavaScript. Happy Star Wars Day and May the 4th be with you!
tags: serverless, javascript, twilio, tutorial
cover_image: https://twilio-cms-prod.s3.amazonaws.com/images/mandoheader2.width-1616.png
---
This blog post was written for [Twilio](https://www.twilio.com/) and [originally published on the Twilio blog](https://www.twilio.com/blog/mandalorian-translator-sms).

Celebrate Star Wars Day on May 4th by texting a sentence to +14807573107 to translate it into the Mandalorian language and read on to learn how to build the app using the [Mandalorian Translator API](https://funtranslations.com/api/mandalorian), [Twilio Functions](https://www.twilio.com/docs/runtime/functions), and the [Twilio Serverless Toolkit](https://www.twilio.com/docs/labs/serverless-toolkit).
![sms example](https://twilio-cms-prod.s3.amazonaws.com/images/1Y6xmX3R4bH3gbLIshNcHyjT4ccm6bwr9K9sXx4eh8nLr.width-1000.png)

### Prerequisites
1. A Twilio account - [sign up for a free one here and receive an extra $10 if you upgrade through this link](http://www.twilio.com/referral/iHsJ5D)
2. A Twilio phone number with SMS capabilities - [configure one here](https://support.twilio.com/hc/en-us/articles/223135247-How-to-Search-for-and-Buy-a-Twilio-Phone-Number-from-Console)
3. [Postman](https://www.postman.com/downloads/) (you could alternatively make cURL requests from the command line)
4. Node.js installed - [download it here](https://nodejs.org/en/download/)

### Make an API Request to Fun Translations 
![kylo approves gif](https://twilio-cms-prod.s3.amazonaws.com/original_images/lOJ9DpnZR7HE-C4-hd1dsZz7VZXVke3MQ7trtIJIsPQ4Y_6gim7voifGQyJedsUZbh9kK3M5_dl2fz)
[Fun Translations](https://funtranslations.com/) offers multiple translator APIs--in addition to the Mandalorian, there's a Yoda translator, pirate speak translator, Dothraki speak translator, and more. To use the Mandalorian translator API, we need to hit this URL: `https://api.funtranslations.com/translate/mandalorian` passing it some text to translate by appending `?text="<TEXT_TO_TRANSLATE_HERE>"`.

Open Postman and paste that URL along with some text in the `<TEXT_TO_TRANSLATE_HERE>` part such as "this is fun" into the URL bar.

Click <strong>Send</strong> to hit it with a GET request to see the following data returned as seen below: 
![postman](https://twilio-cms-prod.s3.amazonaws.com/images/d4M0HW8jHEaZ2P8mUzjhw8TpXaPSyQItjEhu1DmoCBb4r.width-1600.png)
```json
{
    "success": {
        "total": 1
    },
    "contents": {
        "translated": "Ibic is nuhur",
        "text": "this is fun",
        "translation": "mandalorian"
    }
}
```
The free tier only lets you make five API requests an hour. You would need to request an API key in order to use the paid version. The URL would then turn into `https://api.funtranslations.com/translate/mandalorian.json?text=${<TEXT_TO_TRANSLATE_HERE>}&X-Funtranslations-Api-Secret=${YOUR_API_KEY}`, returning data in the same matter as shown below:
![postman with api key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/babuih4e8wdqldwn25x1.png)
```json
{
    "success": {
        "total": 1
    },
    "contents": {
        "translated": "Nynir url ti api key",
        "text": "hit url with api key",
        "translation": "mandalorian"
    }
}
```
You can [upgrade to a paid plan where you can make more HTTP requests here](https://funtranslations.com/api/mandalorian#subscribe), but it's not necessary for this blog post.

### Get Started with the Twilio Serverless Toolkit
The Serverless Toolkit is CLI tooling that helps you develop locally and deploy to [Twilio Runtime](https://www.twilio.com/docs/runtime). The best way to work with the Serverless Toolkit is through the [Twilio CLI](https://www.twilio.com/docs/twilio-cli/quickstart). If you don't have the Twilio CLI installed yet, run the following commands on the command line to install it and the Serverless Toolkit:
```bash
npm install twilio-cli -g
twilio login
twilio plugins:install @twilio-labs/plugin-serverless
```
Create your new project and install our lone requirement `superagent`, an HTTP client library to make HTTP requests in Node.js, by running:
```bash
twilio serverless:init mandalorian-translation
cd mandalorian-translation
npm install superagent
```
If you are on a paid subscription with Fun Translations, open the `.env` file and add an environment variable for your API key. In this blog post below, the API key is called `FUNTRANSLATIONS_API_SECRET`.

### Make a Twilio Function with JavaScript
`cd` into the `\functions` directory and make a new file called `translate.js` containing the following code:
```javascript
const superagent = require('superagent');
var apiurl="https://api.funtranslations.com/translate/mandalorian.json"
exports.handler = function(context, event, callback) {
  const twiml = new Twilio.twiml.MessagingResponse();
  const message = twiml.message(); 
  const msgToTranslate = event.Body.toLowerCase().trim();
  superagent.get(`${apiurl}?text=${msgToTranslate}`) 
  //.set('X-Funtranslations-Api-Secret',context.FUNTRANSLATIONS_API_SECRET) // use this line to reference your API key from a paid Fun Translations plan if you have one
  .end((err, res) => {
    message.body(`"${msgToTranslate}" in Mandalorian translates to "${res.body.contents.translated}". May the 4th be with you!`);
    message.media("https://cdn.pixabay.com/photo/2015/09/04/18/55/yoda-922520_1280.png")
    callback(null, twiml);
  })
  };
```
This code imports `superagent`, makes a [Twilio Messaging Response object](https://www.twilio.com/docs/sms/api/message-resource), retrieves the inbound text message and appends it to the URL we wish to make a HTTP request to. Then the code parses the response from the endpoint (as seen in Postman earlier), to return a text message containing the translated text and an image of Yoda!

You can view the [complete app on GitHub here](https://github.com/elizabethsiegle/mandalorian-language-translator).

### Configure the Function with a Twilio Phone Number
To open up our app to the web with a public-facing URL, go back to the <em>mandalorian-translation</em> root directory and run `twilio serverless:deploy`. You should see this at the bottom of your terminal:
![Function URLs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/62b0pvvllxw775otme8y.png)
In the [phone numbers section of your Twilio Console](https://www.twilio.com/console/phone-numbers/incoming), select the Twilio number you purchased and scroll down to the <em>Messaging</em> section. Under <em>A MESSAGE COMES IN</em> change <em>Webhook</em> to <em>Function</em> and then under <em>Service</em> select <em>Mandalorian-Translation</em>, for <em>Environment</em> select <em>dev-environment</em>, and then for <em>Function Path</em> select <em>/translate</em>.
![Messaging section of phone number](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zpfuic8rd1yvpnmjh3em.png)
Click the <strong>Save</strong> button below and tada! You can now text your Twilio number a phrase and receive the phrase translated from the Mandalorian language back in a response.

### What's Next for Twilio Serverless, APIs, and Star Wars? 
![lol gif](https://i.pinimg.com/originals/a0/90/cc/a090cc71d7d9ef95b22b18b9acc281f0.gif)
How are you celebrating May Fourth? Twilio's Serverless Toolkit makes it possible to deploy web apps quickly, and Twilio Runtime seamlessly handles servers for you.

Let me know online what you're building with Serverless and what your favorite Star Wars quote is! Some of mine include "Never tell me the odds", "Do. Or do not. There is no try", and "Strike me down, and I will become more powerful than you could possibly imagine".
-Twitter: [@lizziepika](http://twitter.com/lizziepika)
- GitHub: [elizabethsiegle](http://github.com/elizabethsiegle)
- Email: lsiegle@twilio.com
- Livestreams: [twitch.tv/lizziepikachu](http://twitch.tv/lizziepikachu)