Skip to content
Advertisement

How to use DuckDuckGo’s search autocomplete suggestions

I’m transitioning my personal search suggestions from google to duckduckgo, but I’m missing something simple to make it work. I’m using jQuery-UI’s autocomplete framework.

My search form

JavaScript

My jQuery

JavaScript

The query for google returns:

JavaScript

The query for duckduckgo returns:

JavaScript

The difference between the two seems to be jsonCallback && jsonCallback([data]) is included in the google query and I don’t understand why they’re different or how to fix it.

EDIT 1

After adding some error handling to the js, the error I’m getting is:

JavaScript

EDIT 2

After digging into this a bit more, I don’t think DDG’s server allows it. It’s my understanding that their server needs to send an appropriate response and I don’t think it’s doing so.

Advertisement

Answer

Please see: https://duckduckgo.com/api

To consume it yourself, you can use one of the language libraries listed below or simply add ‘&format=json’ (or xml if you prefer) onto any query URL in the api subdomain, e.g.

JavaScript

Here are the requirements for use:

  • Attribution in each place you use our API for both us and any underlying source. For the source, you can link to the source’s relevant detail page. For us, you can say Results from DuckDuckGo with our logo (and link to the specific result page).
  • Non-commercial use unless you get email approval from us (though we’re generally fine with anything that isn’t sketchy).
  • Use a descriptive t parameter, i.e. append &t=nameofapp to your requests.

Our overall goal is to get more people using DuckDuckGo, so please keep that in mind as well.

JavaScript

This works successfully with JSONP: https://jsfiddle.net/Twisty/rqdtv9sn/86/

The problem here is that these are not suggestions and the URL for those, https://ac.duckduckgo.com/ac/ does not want to play nice with CORS. You can fudge around it with FETCH API yet this just proceed with the Promise even if the request fails or can’t be parsed.

So until DDG offers a Suggestion API, you’re mostly out of luck.

Some potential other options are discussed here: https://www.sitepoint.com/jsonp-examples/

JavaScript

Although that works, it doesn’t help us much, as we have no way of getting at the data it contains.

Example: https://jsfiddle.net/Twisty/rqdtv9sn/89/

The browser shows the response but then you get a Parse Error.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement