Sample code for 30+ languages & platforms
Unicode C

OpenAI (ChatGPT) Simple Request

See more OpenAI ChatGPT Examples

Demonstrate a simple ChatGPT request with authentication using your OPENAI_API_KEY.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    HCkJsonObjectW json;
    HCkHttpResponseW resp;
    HCkStringBuilderW sbResponseBody;
    HCkJsonObjectW jResp;
    int respStatusCode;
    const wchar_t *Role;
    const wchar_t *Content;
    const wchar_t *finish_reason;
    int index;
    const wchar_t *id;
    const wchar_t *v_object;
    int created;
    const wchar_t *model;
    int Prompt_tokens;
    int Completion_tokens;
    int Total_tokens;
    int i;
    int count_i;

    success = FALSE;

    //  This example assumes the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    http = CkHttpW_Create();

    //  Implements the following CURL command:

    //  curl https://api.openai.com/v1/chat/completions \
    //    -H "Content-Type: application/json" \
    //    -H "Authorization: Bearer $OPENAI_API_KEY" \
    //    -d '{
    //       "model": "gpt-3.5-turbo",
    //       "messages": [{"role": "user", "content": "Say this is a test!"}],
    //       "temperature": 0.7
    //     }'

    //  Use the following online tool to generate HTTP code from a CURL command
    //  Convert a cURL Command to HTTP Source Code

    //  Use this online tool to generate code from sample JSON:
    //  Generate Code to Create JSON

    //  The following JSON is sent in the request body.

    //  {
    //    "model": "gpt-3.5-turbo",
    //    "messages": [
    //      {
    //        "role": "user",
    //        "content": "Say this is a test!"
    //      }
    //    ],
    //    "temperature": 0.7
    //  }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"model",L"gpt-3.5-turbo");
    CkJsonObjectW_UpdateString(json,L"messages[0].role",L"user");
    CkJsonObjectW_UpdateString(json,L"messages[0].content",L"Say this is a test!");
    CkJsonObjectW_UpdateNumber(json,L"temperature",L"0.7");

    //  Adds the "Authorization: Bearer $OPENAI_API_KEY" header.
    //  This is NOT a real key.  Change the "sk-vi...." to your own key.
    CkHttpW_putAuthToken(http,L"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");

    resp = CkHttpResponseW_Create();
    success = CkHttpW_HttpJson(http,L"POST",L"https://api.openai.com/v1/chat/completions",json,L"application/json",resp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkHttpResponseW_Dispose(resp);
        return;
    }

    sbResponseBody = CkStringBuilderW_Create();
    CkHttpResponseW_GetBodySb(resp,sbResponseBody);

    jResp = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(jResp,sbResponseBody);
    CkJsonObjectW_putEmitCompact(jResp,FALSE);

    wprintf(L"Response Body:\n");
    wprintf(L"%s\n",CkJsonObjectW_emit(jResp));

    respStatusCode = CkHttpResponseW_getStatusCode(resp);
    wprintf(L"Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",CkHttpResponseW_header(resp));
        wprintf(L"Failed.\n");
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkHttpResponseW_Dispose(resp);
        CkStringBuilderW_Dispose(sbResponseBody);
        CkJsonObjectW_Dispose(jResp);
        return;
    }

    //  Sample JSON response:
    //  (Sample code for parsing the JSON response is shown below)

    //  {
    //    "id": "chatcmpl-abc123",
    //    "object": "chat.completion",
    //    "created": 1677858242,
    //    "model": "gpt-3.5-turbo-0301",
    //    "usage": {
    //      "prompt_tokens": 13,
    //      "completion_tokens": 7,
    //      "total_tokens": 20
    //    },
    //    "choices": [
    //      {
    //        "message": {
    //          "role": "assistant",
    //          "content": "\n\nThis is a test!"
    //        },
    //        "finish_reason": "stop",
    //        "index": 0
    //      }
    //    ]
    //  }

    //  Sample code for parsing the JSON response...
    //  Use the following online tool to generate parsing code from sample JSON:
    //  Generate Parsing Code from JSON

    id = CkJsonObjectW_stringOf(jResp,L"id");
    v_object = CkJsonObjectW_stringOf(jResp,L"object");
    created = CkJsonObjectW_IntOf(jResp,L"created");
    model = CkJsonObjectW_stringOf(jResp,L"model");
    Prompt_tokens = CkJsonObjectW_IntOf(jResp,L"usage.prompt_tokens");
    Completion_tokens = CkJsonObjectW_IntOf(jResp,L"usage.completion_tokens");
    Total_tokens = CkJsonObjectW_IntOf(jResp,L"usage.total_tokens");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jResp,L"choices");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        Role = CkJsonObjectW_stringOf(jResp,L"choices[i].message.role");
        Content = CkJsonObjectW_stringOf(jResp,L"choices[i].message.content");
        finish_reason = CkJsonObjectW_stringOf(jResp,L"choices[i].finish_reason");
        index = CkJsonObjectW_IntOf(jResp,L"choices[i].index");
        i = i + 1;
    }



    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(json);
    CkHttpResponseW_Dispose(resp);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jResp);

    }