How to use API v8 in suiteCRM 7.10?

i try get /api/v8/modules/viewed on postman and get this error

{
    "errors": [
        {
            "code": 8000,
            "title": "[SuiteCRM] [API] [OAuth] Unable to generate private key",
            "status": 400
        },
        {
            "id": "1",
            "links": {
                "about": null
            },
            "status": 500,
            "code": 8000,
            "title": "JSON API Error",
            "detail": "Api Version: 8",
            "source": {
                "pointer": null,
                "parameter": null
            },
            "meta": {
                "about": "Exception",
                "class": "SuiteCRM\\API\\v8\\Exception\\ApiException",
                "code": 8000,
                "langMessage": null,
                "debug": {
                    "message": "[SuiteCRM] [API] [OAuth] Unable to generate private key",
                    "file": "C:\\xampp\\htdocs\\SuiteCRMxperuri\\lib\\API\\OAuth2\\Keys.php",
                    "line": 91,
                    "trace": [
                        {
                            "file": "C:\\xampp\\htdocs\\SuiteCRMxperuri\\lib\\API\\OAuth2\\Keys.php",
                            "line": 58,
                            "function": "setUpKeys",
                            "class": "SuiteCRM\\API\\OAuth2\\Keys",
                            "type": "->",
                            "args": []
                        },
                        {
                            "file": "C:\\xampp\\htdocs\\SuiteCRMxperuri\\lib\\API\\v8\\container\\ResourceServer.php",
                            "line": 46,
                            "function": "getPublicKey",
                            "class": "SuiteCRM\\API\\OAuth2\\Keys",
                            "type": "->",
                            "args": []
                        },
                        {
                            "file": "C:\\xampp\\htdocs\\SuiteCRMxperuri\\vendor\\pimple\\pimple\\src\\Pimple\\Container.php",
                            "line": 118,
                            "function": "{closure}",
                            "args": [
                                {}
                            ]
                        },
                        {
                            "file": "C:\\xampp\\htdocs\\SuiteCRMxperuri\\vendor\\slim\\slim\\Slim\\Container.php",
                            "line": 123,
                            "function": "offsetGet",
                            "class": "Pimple\\Container",
                            "type": "->",
                            "args": [
                                "ResourceServer"
                            ]
                        },
                        {
                            "file": "C:\\xampp\\htdocs\\SuiteCRMxperuri\\vendor\\slim\\slim\\Slim\\CallableResolver.php",
                            "line": 86,
                            "function": "get",
                            "class": "Slim\\Container",
                            "type": "->",
                            "args": [
                                "ResourceServer"
                            ]
                        },
                        {
                            "file": "C:\\xampp\\htdocs\\SuiteCRMxperuri\\vendor\\slim\\slim\\Slim\\CallableResolver.php",
                            "line": 67,
                            "function": "resolveCallable",
                            "class": "Slim\\CallableResolver",
                            "type": "->",
                            "args": [
                                "ResourceServer"
                            ]
                        },
                        {
                            "file": "C:\\xampp\\htdocs\\SuiteCRMxperuri\\vendor\\slim\\slim\\Slim\\CallableResolverAwareTrait.php",
                            "line": 45,
                            "function": "resolve",
                            "class": "Slim\\CallableResolver",
                            "type": "->",
                            "args": [
                                "ResourceServer"
                            ]
                        },
                        {
                            "file": "C:\\xampp\\htdocs\\SuiteCRMxperuri\\vendor\\slim\\slim\\Slim\\DeferredCallable.php",
                            "line": 36,
                            "function": "resolveCallable",
                            "class": "Slim\\DeferredCallable",
                            "type": "->",
                            "args": [
                                "ResourceServer"
                            ]
                        },
                        {
                            "function": "__invoke",
                            "class": "Slim\\DeferredCallable",
                            "type": "->",
                            "args": [
                                {},
                                {},
                                {}
                            ]
                        },
                        {
                            "file": "C:\\xampp\\htdocs\\SuiteCRMxperuri\\vendor\\slim\\slim\\Slim\\MiddlewareAwareTrait.php",
                            "line": 70,
                            "function": "call_user_func",
                            "args": [
                                {},
                                {},
                                {},
                                {}
                            ]
                        },
                        {
                            "file": "C:\\xampp\\htdocs\\SuiteCRMxperuri\\vendor\\slim\\slim\\Slim\\MiddlewareAwareTrait.php",
                            "line": 117,
                            "function": "Slim\\{closure}",
                            "class": "Slim\\App",
                            "type": "->",
                            "args": [
                                {},
                                {}
                            ]
                        },
                        {
                            "file": "C:\\xampp\\htdocs\\SuiteCRMxperuri\\vendor\\slim\\slim\\Slim\\App.php",
                            "line": 406,
                            "function": "callMiddlewareStack",
                            "class": "Slim\\App",
                            "type": "->",
                            "args": [
                                {},
                                {}
                            ]
                        },
                        {
                            "file": "C:\\xampp\\htdocs\\SuiteCRMxperuri\\vendor\\slim\\slim\\Slim\\App.php",
                            "line": 314,
                            "function": "process",
                            "class": "Slim\\App",
                            "type": "->",
                            "args": [
                                {},
                                {}
                            ]
                        },
                        {
                            "file": "C:\\xampp\\htdocs\\SuiteCRMxperuri\\lib\\API\\core\\app.php",
                            "line": 89,
                            "function": "run",
                            "class": "Slim\\App",
                            "type": "->",
                            "args": []
                        },
                        {
                            "file": "C:\\xampp\\htdocs\\SuiteCRMxperuri\\lib\\API\\public\\index.php",
                            "line": 5,
                            "args": [
                                "C:\\xampp\\htdocs\\SuiteCRMxperuri\\lib\\API\\core\\app.php"
                            ],
                            "function": "require_once"
                        }
                    ],
                    "traceAsString": "#0 C:\\xampp\\htdocs\\SuiteCRMxperuri\\lib\\API\\OAuth2\\Keys.php(58): SuiteCRM\\API\\OAuth2\\Keys->setUpKeys()\n#1 C:\\xampp\\htdocs\\SuiteCRMxperuri\\lib\\API\\v8\\container\\ResourceServer.php(46): SuiteCRM\\API\\OAuth2\\Keys->getPublicKey()\n#2 C:\\xampp\\htdocs\\SuiteCRMxperuri\\vendor\\pimple\\pimple\\src\\Pimple\\Container.php(118): {closure}(Object(Slim\\Container))\n#3 C:\\xampp\\htdocs\\SuiteCRMxperuri\\vendor\\slim\\slim\\Slim\\Container.php(123): Pimple\\Container->offsetGet('ResourceServer')\n#4 C:\\xampp\\htdocs\\SuiteCRMxperuri\\vendor\\slim\\slim\\Slim\\CallableResolver.php(86): Slim\\Container->get('ResourceServer')\n#5 C:\\xampp\\htdocs\\SuiteCRMxperuri\\vendor\\slim\\slim\\Slim\\CallableResolver.php(67): Slim\\CallableResolver->resolveCallable('ResourceServer')\n#6 C:\\xampp\\htdocs\\SuiteCRMxperuri\\vendor\\slim\\slim\\Slim\\CallableResolverAwareTrait.php(45): Slim\\CallableResolver->resolve('ResourceServer')\n#7 C:\\xampp\\htdocs\\SuiteCRMxperuri\\vendor\\slim\\slim\\Slim\\DeferredCallable.php(36): Slim\\DeferredCallable->resolveCallable('ResourceServer')\n#8 [internal function]: Slim\\DeferredCallable->__invoke(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response), Object(Slim\\App))\n#9 C:\\xampp\\htdocs\\SuiteCRMxperuri\\vendor\\slim\\slim\\Slim\\MiddlewareAwareTrait.php(70): call_user_func(Object(Slim\\DeferredCallable), Object(Slim\\Http\\Request), Object(Slim\\Http\\Response), Object(Slim\\App))\n#10 C:\\xampp\\htdocs\\SuiteCRMxperuri\\vendor\\slim\\slim\\Slim\\MiddlewareAwareTrait.php(117): Slim\\App->Slim\\{closure}(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response))\n#11 C:\\xampp\\htdocs\\SuiteCRMxperuri\\vendor\\slim\\slim\\Slim\\App.php(406): Slim\\App->callMiddlewareStack(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response))\n#12 C:\\xampp\\htdocs\\SuiteCRMxperuri\\vendor\\slim\\slim\\Slim\\App.php(314): Slim\\App->process(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response))\n#13 C:\\xampp\\htdocs\\SuiteCRMxperuri\\lib\\API\\core\\app.php(89): Slim\\App->run()\n#14 C:\\xampp\\htdocs\\SuiteCRMxperuri\\lib\\API\\public\\index.php(5): require_once('C:\\\\xampp\\\\htdocs...')\n#15 {main}"
                }
            }
        }
    ],
    "meta": {
        "suiteapi": {
            "major": 8,
            "minor": 0,
            "patch": 0,
            "stability": "ALPHA"
        }
    },
    "jsonapi": {
        "version": "1.0"
    }
}

Can Help how to use API in suitecrm?

Have you checked the Apache error log?

i test it with postman and get this error. do you have tutorial to use API suitecrm? or what i was config to use API? thanks

I think the problem is server-side (permissions maybe?), PHP is calling openssl_pkey_new() but the call fails. You should check the server logs

the same mistake and I couldn’t fix it. Could you?

Have you checked the server logs?

this appears but I don’t know if it’s affecting

Have you followed this exact steps in the url to set up the private / public.key for the SuiteCRM instance ?

https://docs.suitecrm.com/developer/api/developer-setup-guide/json-api/#_before_you_start_calling_endpoints

The instance needs to be HTTPS.

Hey @zan1515 , I did all those steps, changed the constant and created the keys. My instance is on HTTPS.

So i tried to track what is happening here and it seems when it tries to read the private key in the directory it fails and reverts to creating a new private key.

It then goes through the process of creating a new key and uses function

openssl_pkey_new

this is the logic it tries to run inside \lib\API\OAuth2\Keys.php when it does not find the private.key :

/**
 * @throws ApiException
 */
private function setUpKeys()
{
        $config = array(
            'digest_alg' => 'sha512',
            'private_key_bits' => '2048',
            'private_key_type' => OPENSSL_KEYTYPE_RSA,
        );

        // Create the private and public key
        $resource = openssl_pkey_new($config);

        if($resource === false) {
            throw new ApiException('[OAuth] Unable to generate private key');
        }

        // Extract the private key from $res to $privKey
        openssl_pkey_export($resource, $privateKey);
        openssl_pkey_export_to_file($privateKey, __DIR__.'/private.key');

        // Extract the public key from $res to $pubKey
        $publicKey = openssl_pkey_get_details($resource);
        if(!isset($publicKey['key'])) {
                throw new ApiException('[OAuth] Unable to generate public key');
        }
        file_put_contents(__DIR__.'/public.key', $publicKey['key']);
}

And inside you can see it throws the error “[OAuth] Unable to generate private key”, so it would be smart to check why the function fails.

Try checking the PHP docs for this particular function :
https://www.php.net/manual/en/function.openssl-pkey-new.php

Hope this helps

Assuming you have generated your public.key and private.key with openssl, did you set permissions properly on your public.key and private.key files that you generated in your /Api/V8/OAuth2 folder so that apache can access them?

You can set permissions with these 2 commands:

sudo chmod 600 private.key public.key
sudo chown www-data:www-data p*.key

If you haven’t generated public.key and private.key navigate to your /Api/V8/OAuth2 folder and do the following commands:

openssl genrsa -out private.key 2048
openssl rsa -in private.key -pubout -out public.key

Then go set the file permissions.