Skip to content

Many people know about the internet Bodhisattva, that's right, it's Cloudflare. Almost all of its products, including but not limited to CDN, KV database, Pages, and Workers, provide a large amount of free resources and encourage people to use them for free. More importantly, the free quota is very high. Generally, small businesses or individual studios can support their businesses without spending a penny.

Recently, Cloudflare launched a free AI service. Directly in their website backend, without a server or domain name, you can easily set up a free and usable AI service in a few simple steps, including translation, text generation, text-to-image generation, and speech recognition. It's a real boon, don't miss it!

This article mainly introduces how to build a usable free translation API in the Cloudflare backend using the m2m100-1.2B model. The effect after implementation is as follows:

This article mainly includes:

  1. Register an account and log in
  2. Create a Worker service
  3. Copy and paste the code and deploy it
  4. Bind your own domain (optional)
  5. Use this API in code or software

1. Register an account and log in

If you don't have an account, click this link to register. It's very simple. https://dash.cloudflare.com/sign-up

If you have an account, log in directly: https://dash.cloudflare.com/login

2. Create a Worker service

After logging in, click "Workers and Pages" on the left.

Then click "Create".

In the new page that opens, click "AI" on the right, find "Translation App", hover your mouse over it, and click.

In the next interface, set a name. This name will be part of the domain name assigned to you. Only numbers, English letters, underscores, and hyphens are allowed, as shown in the figure below, and then save -- complete.

Next, you will be told that the deployment was successful, but you still need to paste the code to replace the default one.

3. Copy and paste the code

Click "Edit code" shown at the end of the previous step, wait for the page to display, then delete all the code on the left, and paste the following code to the left. 123456 is your access key, you can modify it to prevent others from using it and wasting your free traffic.

// This is the access key
const SECRET_PASS="123456"

export default {
  async fetch(request, env) {
    const urlStr = request.url
    const urlObj = new URL(urlStr)
    let text =  urlObj.searchParams.get('text')
    let source_language = urlObj.searchParams.get('source_language')
    let target_language = urlObj.searchParams.get('target_language')
    let secret = urlObj.searchParams.get('secret')
    if(secret!==SECRET_PASS){
      return Response.json({code:1,msg:"Unauthorized access",text:text,source_language:source_language,target_lanuage:target_language,secret:secret});
    }
    const inputs = {
      text: text,
      source_lang: source_language,
      target_lang: target_language,
    };
    const response = await env.AI.run('@cf/meta/m2m100-1.2b', inputs);

    if(response.translated_text.indexOf('ERROR')===0){
      return Response.json({code:2,msg:"ok",text:response.translated_text});
    }
    return Response.json({code:0,msg:"ok",text:response.translated_text });

  },
};

After pasting, wait until "Save and Deploy" in the upper right corner is clickable, then click it, and in the confirmation window that pops up, confirm and deploy again.

Okay, you can use it happily. For example, my deployed address is https://my-translate-api.2124455076.workers.dev/, so I can use this address directly:

https://my-translate-api.2124455076.workers.dev/?text=%E4%BD%A0%E5%A5%BD%E5%95%8A%E6%88%91%E7%9A%84%E6%9C%8B%E5%8F%8B&source_language=zh&target_language=en&secret=123456

This address is your translation API interface address.

4. Bind your own domain

If you think this address is too long, or workers.dev may be blocked and inaccessible in China, and you don't want to use a proxy, you can bind your own domain.

First, you need to change your domain's NS servers to Cloudflare's. After it takes effect, go back to the Cloudflare homepage: https://dash.cloudflare.com/, and bind your domain.

NSezra.ns.cloudflare.com
NSkarsyn.ns.cloudflare.com

After adding it, go back to your worker. Remember how to enter? Click "Workers and Pages" on the left.

You will see the service you have created on the right. Click its name to enter the settings page.

As shown in the figure below, first click "Settings -> Triggers", then click "Add Custom Domain", you can add your own domain. For example, if your domain bound to Cloudflare is abc.com, you can set api.abc.com here.

Okay, now you can use your own domain name to access it.

5. Using this API in code and software

First, remember the language codes supported by m2m100:

Afrikaans (af), Amharic (am), Arabic (ar), Asturian (ast), Azerbaijani (az), Bashkir (ba), Belarusian (be), Bulgarian (bg), Bengali (bn), Breton (br), Bosnian (bs), Catalan (ca), Cebuano (ceb), Czech (cs), Welsh (cy), Danish (da), German (de), Greek (el), English (en), Spanish (es), Estonian (et), Persian (fa), Fula (ff), Finnish (fi), French (fr), West Frisian (fy), Irish (ga), Scottish Gaelic (gd), Galician (gl), Gujarati (gu), Hausa (ha), Hebrew (he), Hindi (hi), Croatian (hr), Haitian Creole (ht), Hungarian (hu), Armenian (hy), Indonesian (id), Igbo (ig), Ilocano (ilo), Icelandic (is), Italian (it), Japanese (ja), Javanese (jv), Georgian (ka), Kazakh (kk), Khmer (km), Kannada (kn), Korean (ko), Luxembourgish (lb), Ganda (lg), Lingala (ln), Lao (lo), Lithuanian (lt), Latvian (lv), Malagasy (mg), Macedonian (mk), Malayalam (ml), Mongolian (mn), Marathi (mr), Malay (ms), Burmese (my), Nepali (ne), Dutch (nl), Norwegian (no), Northern Sotho (ns), Occitan (oc), Oriya (or), Punjabi (pa), Polish (pl), Pashto (ps), Portuguese (pt), Romanian (ro), Russian (ru), Sindhi (sd), Sinhala (si), Slovak (sk), Slovenian (sl), Somali (so), Albanian (sq), Serbian (sr), Swati (ss), Sundanese (su), Swedish (sv), Swahili (sw), Tamil (ta), Thai (th), Tagalog (tl), Tswana (tn), Turkish (tr), Ukrainian (uk), Urdu (ur), Uzbek (uz), Vietnamese (vi), Wolof (wo), Xhosa (xh), Yiddish (yi), Yoruba (yo), Chinese (zh), Zulu (zu)

Using this API with Python code

import requests
response = requests.get(url="https://transapi.pyvideotrans.com/?text=你好啊我的朋友们&source_language=zh&target_language=en&secret=123456")

print(response.json())

It's that simple.

Using it in video translation software

Open the settings menu in the upper left corner -- Custom translation API, fill in your API address and key, and then test it.

After there are no problems, select "TransAPI" in the translation channel to happily use free API and free service resources to translate your videos. The free quota is usually enough for monthly use.

Thanks again to the internet Bodhisattva.