Announcement

Collapse
No announcement yet.

API example

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • API example

    Hello

    It would be very helpful if someone would post a complete working api example file here in the forum. It should create as a record of a standard entity (Case, Account,..). Maybe in PHP.

    This pattern could be used for comparison and testing.

    Thank you very much in advance. I'm really looking forward to hearingfrom you.
    Best wishes,

    Peter

  • #2
    Hello,
    look github of espocrm .. Yuri have make many documentation on howto development with many sample

    EspoCRM Documentation. Contribute to espocrm/documentation development by creating an account on GitHub.

    .

    Comment


    • peterberlin
      peterberlin commented
      Editing a comment
      Helllo Item
      Thanks a lot for your hint.

  • #3
    Hi Peter,

    Here's how to use PHP client https://github.com/espocrm/documenta...t-php.md#usage

    API functions for Account entity type: https://github.com/espocrm/documenta...api/account.md

    How to make search: https://github.com/espocrm/documenta...arch-params.md

    Comment


    • #4
      Hi, Yuri,
      Thank you very much.
      I have read your documentation and understand the content. But since I don't know the REST API, there is a very simple question.
      The code under "Usage" is the minimum code you need and can extend it or is it just a code snippet?

      If you know what all the code looks like that works, you can customize it for your own themes.
      peter

      Comment


      • #5
        Originally posted by peterberlin View Post
        Hello
        It would be very helpful if someone would post a complete working api example file here in the forum. It should create as a record of a standard entity (Case, Account,..). Maybe in PHP.

        This pattern could be used for comparison and testing.
        The web interface uses the REST API for all its calls. I find it very useful to load the Network tab of hte chrome console and just do things, you will see there what API calls are involved in anything you.

        For instance, creating an Account calls api/v1/Account

        Note that it is specially usefull when managing attachments, documents or related records as you will see there are more than one single call involved.

        Comment


        • #6
          I agree with peterberlin that the API documentation is hard to follow, specially for someone like myself who has very little experience building APIs, but the actual implementation is like a lot of great Espo features, very simple yet very powerful.

          These are the steps that I took, based on the documentation, to implement an API call to create a new Contact from an external PHP script:

          At EspoCRM:

          1) Go to Administration > API Users and create a new API user (or use an existing one if you prefer) and write down the generated API key.
          2) Go to Administration > Roles and create a new Role for the API User (for example "Contact Agent")
          3) Assign the appropriate permissions to the "Contact Agent" role (for example Create, Read, Update Contact entity only)
          4) Link the "Contact Agent" role with the API user

          At my external application:

          1) Create a new script: espo_api_client.php, copy and paste the code for this script from the documentation's EspoApiClient class. https://github.com/espocrm/documenta...t-php.md#class to handle the mechanics of performing the API calls.

          API calls are actually implemented using the PHP curl functions but these are quite complicated so the documentation provides a wrap class (EspoApiClient) to perform the calls with minimum coding.

          2) Create the php script where I want to implement the API call and process any results if applicable. In my case the code here is:
          PHP Code:
          <?php

          require_once('espo_api_client.php');

          $my_url '{{https://your espo web address}} ';
          $api_key '{{here I put the API string generated by Espo for the API user}}';

          $client = new EspoApiClient($my_url);
          $client->setApiKey($api_key);

          $new_contact_data = ['firstName' => '{{new contact first name}}''lastName' => '{{new contact last name}}'];

          $reponse $client->request('POST''Contact'$new_contact_data);
          That's it ! as I mentioned quite easy to implement thanks to the EspoApiClient wrap class.
          Last edited by telecastg; 04-13-2020, 06:27 PM.

          Comment


          • peterberlin
            peterberlin commented
            Editing a comment
            Hi telecastg,
            thank you very much.
            Your tips will certainly help many users who have no experience with the API.
            But my thanks also go to the ESPO developers, who have extended the documentation in many places .
            peter

          • telecastg
            telecastg commented
            Editing a comment
            You're welcome

            I also appreciate very much the time taken by the developers to provide documentation.

            It is quite understandable that writing instructions for a process that is very familiar to one author makes it very hard to describe in a way that someone without experience can easily grasp. That is why we should encourage EVERYONE to help by posting examples as much as possible.

          • esforim
            esforim commented
            Editing a comment
            For forum searching keyword.

            API_

        • #7
          Can you please tell me which url is to be used and what is the use of this api how can i ustilize it. Im new please help

          Comment


          • #8
            When I run the js client on node.js I get:

            Code:
            (node:79836) UnhandledPromiseRejectionWarning: #<IncomingMessage>
            at emitUnhandledRejectionWarning (internal/process/promises.js:151:15)
            at processPromiseRejections (internal/process/promises.js:211:11)
            at processTicksAndRejections (internal/process/task_queues.js:98:32)
            (node:79836) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
            (node:79836) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
            at emitDeprecationWarning (internal/process/promises.js:161:11)
            at processPromiseRejections (internal/process/promises.js:213:13)
            at processTicksAndRejections (internal/process/task_queues.js:98:32)

            The strange part is that when I run

            Code:
            var data = {
              maxSize: 1,
              where: [
                {
                  type: 'equals',
                  attribute: 'type',
                  value: 'Customer'
                }
              ],
              select: 'id,name',
              };
            
            const querystring = require('querystring');
            console.log(querystring.stringify(data));
            I get:
            Code:
            maxSize=1&where=&select=id%2Cname

            That is, the where clause is not parsed properly. As I am trying to port the client to outside node.js (in my case Google Apps Script) all help would be appreciated.
            Last edited by arthurmaciel; 06-29-2020, 03:06 AM.

            Comment

            Working...
            X