How to use the DeepL API from Drafts?

The DeepL API can be accessed using several client libraries or by a HTTP request.

I’d like to replace the whole draft (if no text is selected) or the selected text with the translation from DeepL.

Can someone please give me guidance on how to build such a request from Drafts and how to read out the response?

{
  "translations": [
    {
      "detected_source_language": "EN",
      "text": "Hallo, Welt!"
    }
  ]
}

Thanks!

1 Like

Check these out:

2 Likes

@tomtjes’ pointers are the right direction, generally. Drafts can make HTTP requests with it’s scripting interface. Do you have experience scripting access to APIs in general? The HTTP object works nicely with JSON-based APIs, and will even parse the result for you.

As far as the integration on Drafts’ end, the editor object (docs) has methods to get the selected text to pass to the API call, and to update it once you have received and parsed a result.

I created an Action that uses the DeepL API for translation → DeepL Translate
Let me know if that is what you wanted :slight_smile: Thanks for the inspiration

2 Likes

Thanks for your great work. Once the script is finished, it all looks very straightforward to me, a non-programmer :wink:

1 Like