Oracle APEX Cohere Integration Workaround – Using v2 Chat Endpoint with Web Credentials

AI inside Oracle APEX is no longer just an experiment – it’s practical, powerful, and usable today. But here’s the catch: the OOTB Cohere integration in Oracle APEX currently relies on the v1/chat endpoint, which Cohere has officially deprecated. This creates problems if you try to use it as-is.

Solving the APEX-Cohere Puzzle: A Smart Workaround with v2 Chat and Web Credentials 💡
⚠️ Disclaimer: This post demonstrates a workaround for Oracle APEX versions less than 24.2.9. I’m doing this to make existing things work and because the Free Cloud tier currently still has version 24.2.8. If you’re using APEX’s built-in Generative AI Services, note that they currently won’t work correctly with Cohere due to the deprecated endpoint.

So in this post, I’ll show you a workaround: how to connect APEX directly to Cohere’s new v2/chat endpoint. This gives you two big advantages:

  1. You’re no longer stuck with the deprecated OOTB service.
  2. You can freely use any AI Service provider's endpoint and any model of your choice as this methodology is generic for all others.

🎥 Watch It in Action


🔑 Step 1: Prepare Your Cohere API Key

Before we can start building in APEX:

  1. Log in to Cohere Dashboard.
  2. Copy your API Key (already generated by default).
  3. Check the Cohere API v2 documentation to see which models are available.

🔐 Step 2: Create a Web Credential in APEX

We’ll store our API key securely using Web Credentials in APEX. This makes sure your key isn’t hardcoded in PL/SQL.

  1. Navigate to Shared Components → Security → Web Credentials.
  2. Create a new Web Credential.
  3. Set Credential Name to: Authorization.
  4. For Credential Secret, paste your API Key prefixed with Bearer , like:
    Bearer sk-your-api-key
  5. Save the credential.

Now this credential can be referenced safely in PL/SQL.


📄 Step 3: Create the APEX Page

Inside your APEX application:

  • Create a Blank Page.
  • Add a Text ItemP11_INPUT (user prompt).
  • Add a Rich Text EditorP11_RESPONSE (to display formatted AI response).
  • Add a ButtonAsk AI.

⚙️ Step 4: Add JavaScript to Call APEX Ajax

Include the marked.js library to convert Markdown to HTML. Add this in the page's JavaScript File URLs section:

https://cdn.jsdelivr.net/npm/marked/marked.min.js

Then add the following JS for the button:

function callCohereAI() {
  const promptText = $v("P11_INPUT");

  if (!promptText) {
    alert("Please enter a prompt!");
    return;
  }

  // Call APEX Ajax Callback
  apex.server.process(
    "CALL_COHERE_API",
    { x01: promptText },
    {
      dataType: "json",
      success: function (data) {
        const assistantText = data.message?.content?.[0]?.text || "(No text returned)";
        const formattedHTML = marked.parse(assistantText);
        $s("P11_RESPONSE", formattedHTML);
      },
      error: function (xhr, status, error) {
        console.error(error);
        $s("P11_RESPONSE", "Error: " + error);
      }
    }
  );
}

// Trigger function
callCohereAI();

🖇️ Step 5: PL/SQL Ajax Callback

Here’s the Ajax process that calls Cohere’s v2/chat endpoint using APEX_WEB_SERVICE.

DECLARE
    l_response   clob;
    l_prompt     varchar2(4000) := apex_application.g_x01;
BEGIN
    -- Set request headers
    apex_web_service.g_request_headers.delete;
    apex_web_service.g_request_headers(1).name  := 'Content-Type';
    apex_web_service.g_request_headers(1).value := 'application/json';

    -- Build JSON payload
    l_response := json_object(
                     'model' VALUE 'command-a-03-2025',
                     'messages' VALUE json_array(
                         json_object(
                             'role' VALUE 'user',
                             'content' VALUE l_prompt
                         )
                     )
                   RETURNING VARCHAR2
                   );

    -- Debug
    apex_debug.message('Cohere Request JSON: %s', l_response);

    -- REST call using Web Credential
    l_response := apex_web_service.make_rest_request(
                     p_url                  => 'https://api.cohere.ai/v2/chat',
                     p_http_method          => 'POST',
                     p_body                 => l_response,
                     p_credential_static_id => 'credentials_for_cohere_test_connection'
                  );

    -- Debug raw response
    apex_debug.message('Cohere Response JSON: %s', l_response);

    -- Return JSON back to JS
    htp.p(rtrim(l_response));
END;

✅ Step 6: Test the Setup

Run the page, type a question in P11_INPUT, click Ask AI, and watch as Cohere replies in rich text format directly inside your APEX app.


🚀 Conclusion

Oracle APEX’s built-in Cohere integration currently points to a deprecated endpoint, causing issues in versions less than 24.2.9. By implementing this PL/SQL + Ajax + Web Credential workaround, you not only fix that problem but also gain the flexibility to use any Cohere/other Service provider's endpoint (chat, embed etc.) you want.

This approach keeps your apps future-proof, gives you more control, and ensures AI can be embedded safely and efficiently. Stay tuned for more posts where I explore deeper integrations and enhancements with Generative AI in Oracle APEX.

data:post.title

Oracle APEX Cohere Integration Workaround – Using v2 Chat Endpoint with Web Credentials

Written by JENISH JOYAL J

Published on October 01, 2025

No comments:

Powered by Blogger.