Forum Discussion

Inactive User's avatar
Inactive User
6 years ago

Can I call a V2 API url in Javascript?

I want to make a call to the Lithium API via Javascript. I know I can do it with V1; can I do with with V2 as well? 

Something like 

"/restapi/2.0/search?q="SELECT * FROM messages WHERE labels.text IN ('Jupiter')"

 I want to be able to select messages by tag.id or tag.text (preferably id, if possible), called from Javascript. 

  • Inactive User 
    Yes, you can make a V2 hit from JS also. But URL will be like this
    For stage

     

    https://api.stage.lithium.com/community/2.0/<tenant_id>/search?q=SELECT+*+FROM+messages+LIMIT+1&api.pretty_print=true&client-id=xxxxxxxxxx

     

    For production

    https://api.lithium.com/community/2.0/<tenant_id>/search?q=SELECT+*+FROM+messages+LIMIT+1&api.pretty_print=true&client-id=LFE0UbsMFc9uDhsZLu03q77KZZjO78svnKTGyT6FDRs=

    Client id can be generated from App under Admin > System > API Apps 


    For authentication, you may need to pass the session id also which can be generated using below API by passing username and password

    https://<community_domain>/restapi/vc/authentication/sessions/login
  • Inactive User

    Lithium introduced endpoints to get it done. 

  • Hi Inactive User ,

    Yes this is a thing. I myself do them all the time. To be able to do this from a page on your community you can use the URL above. I, however, use this one;

    https://<community_domain>/<community_id>/api/2.0/search?q=

    The bit after the *q* parameter has to be encoded using the encodeURIComponent() in Javascript.

     

    Sources:

    API V2 Documentation

    Mozilla Developer - EncodeURIComponent

    • VikasB's avatar
      VikasB
      Boss

      Inactive User 
      Yes, you can make a V2 hit from JS also. But URL will be like this
      For stage

       

      https://api.stage.lithium.com/community/2.0/<tenant_id>/search?q=SELECT+*+FROM+messages+LIMIT+1&api.pretty_print=true&client-id=xxxxxxxxxx

       

      For production

      https://api.lithium.com/community/2.0/<tenant_id>/search?q=SELECT+*+FROM+messages+LIMIT+1&api.pretty_print=true&client-id=LFE0UbsMFc9uDhsZLu03q77KZZjO78svnKTGyT6FDRs=

      Client id can be generated from App under Admin > System > API Apps 


      For authentication, you may need to pass the session id also which can be generated using below API by passing username and password

      https://<community_domain>/restapi/vc/authentication/sessions/login