Issue saving OpenAI API response to the database in Server Connect

Hey guys, I'm having trouble saving the response from the OpenAI API to the database using Server Connect. The API request itself is working fine — the response comes back correctly, and I can see it in the console without any issues.

The problem is when I try to save the value (api_openai.data.choices[0].message.content) to the database using the "Database Insert" step. Nothing gets saved.

Setup:

  • The API request is configured correctly (POST method, Authorization and Content-Type headers are set)

  • The response from the API is a valid string (I confirmed this using console)

  • The database field is set as text

API output:

{
    "api_openai": {
        "status": 200,
        "headers": {
            "date": "Wed, 12 Mar 2025 11:27:59 GMT",
            "content-type": "application/json",
            "transfer-encoding": "chunked",
            "connection": "keep-alive",
            "access-control-expose-headers": "X-Request-ID",
            "openai-organization": "improve-ai-aqyo3v",
            "openai-processing-ms": "453",
            "openai-version": "2020-10-01",
            "x-ratelimit-limit-requests": "10000",
            "x-ratelimit-limit-tokens": "200000",
            "x-ratelimit-remaining-requests": "9999",
            "x-ratelimit-remaining-tokens": "199973",
            "x-ratelimit-reset-requests": "8.64s",
            "x-ratelimit-reset-tokens": "7ms",
            "x-request-id": "req_7789b318a4f780e2bbf354e31c42e",
            "strict-transport-security": "max-age=31536000; includeSubDomains; preload",
            "cf-cache-status": "DYNAMIC",
            "set-cookie": [
                "__cf_bm=NRPacFxdWIwv7x3rmhNykBcne0geAV8aMigFbSa.g-1741778879-1.0.1.1-7XIOa21cxX7NRq6o.1QlHuxKUfmhFPRJ1a82_0jucrB893x480MA5CVRcLJkO1HKF6fT_ajbJOX3aB169szwfQBcmpyY2CD4UDjCIojnmhk; path=/; expires=Wed, 12-Mar-25 11:57:59 GMT; domain=.api.openai.com; HttpOnly; Secure; SameSite=None",
                "_cfuvid=dFAGLYmbGdjMJLSQml1ClV2a14QnLCBlLNDumEHnSEY-1741778879653-0.0.1.1-604800000; path=/; domain=.api.openai.com; HttpOnly; Secure; SameSite=None"
            ],
            "x-content-type-options": "nosniff",
            "server": "cloudflare",
            "cf-ray": "91f2f209ab1df1b3-GRU",
            "alt-svc": "h3=\":443\"; ma=86400"
        },
        "data": {
            "id": "chatcmpl-BAEXfZkDXhXuzq3Z3E4pStbgrdhr8",
            "object": "chat.completion",
            "created": 1741778879,
            "model": "gpt-4o-mini-2024-07-18",
            "choices": [
                {
                    "index": 0,
                    "message": {
                        "role": "assistant",
                        "content": "Hello! It looks like you're testing the system. How can I assist you today?",
                        "refusal": null,
                        "annotations": []
                    },
                    "logprobs": null,
                    "finish_reason": "stop"
                }
            ],
            "usage": {
                "prompt_tokens": 18,
                "completion_tokens": 18,
                "total_tokens": 36,
                "prompt_tokens_details": {
                    "cached_tokens": 0,
                    "audio_tokens": 0
                },
                "completion_tokens_details": {
                    "reasoning_tokens": 0,
                    "audio_tokens": 0,
                    "accepted_prediction_tokens": 0,
                    "rejected_prediction_tokens": 0
                }
            },
            "service_tier": "default",
            "system_fingerprint": "fp_06737906"
        }
    }
}

How can I get the value from the API response to be recognized correctly and stored in the database?

I think the issue might be that the value is not available yet when the insert is triggered — possibly because the API call is asynchronous. If that's the case, how can I create an async function in Server Connect to handle this properly?

This probably wont work as the API call needs to be inside a promise or the next step may be run before completion?

Try adding a set value step after the API call and output the value of message content and see what it is at that stage

I added the "Set Value" after the API call and also tried removing the second Try/Catch block. I got the response, just like before, but it's still not being inserted into the database.

I'm studying this... still don't know how to work with promises and async functions.

I don't know where this "async" talk came from :laughing:

I'm betting it's a variable scope bug with using nested Try/Catch or something like that. Try doing everything without a Try/Catch and see if it works

Hi Apple, the async issue is just a guess on my part, and it’s also somewhat based on that discussion: Passing parameter to API call in serverconnect

Regarding the removal of the Try/Catch block structure, the situation actually gets a bit worse — a server action error occurs, and the value doesn’t even receive the API response.

The situation gets better, because now you know it's an issue with your database tables/columns, it has nothing to do with the API Action :wink:

The Try/Catch was masking the issue and you didn't realize it.

As for the database issue, are you using Wappler database manager to create tables? Make sure all Changes are run, and if they are then I suggest erasing the DB and running the migrations again.

1 Like

Oh! That makes a lot of sense. I am indeed using the database manager, which is why it feels strange, since everything I created was done dynamically.

However, I'll follow your advice and investigate this more deeply. Thanks for the tip.

Your response was spot on, @Apple!

It turns out I was using the wrong Database Server. I fixed the database connection, and now it's working perfectly.

Also, big thanks to @Hyperbytes for stepping in to help! I spent many hours on this yesterday, so this was a huge help.

1 Like

You using php or node?

Node

Considered this extension to make life easier? (DeepSeek will be added this weekend after Fridays meetup)

I considered it and even implemented it — thanks for the input! However, since I'm developing this for learning purposes, I think it’s quite useful to stick with a more traditional approach.

1 Like