# An error occurred: Unexpected token '\<', "\<!DOCTYPE "... is not valid JSON

**URL:** https://community.openai.com/t/an-error-occurred-unexpected-token-doctype-is-not-valid-json/359906
**Category:** API
**Tags:** api
**Created:** [September 8, 2023, 8:59am UTC](https://community.openai.com/t/an-error-occurred-unexpected-token-doctype-is-not-valid-json/359906 "2023-09-08T08:59:34Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![akshaymwf](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/akshaymwf/32/146549_2.png) [@akshaymwf](https://community.openai.com/u/akshaymwf)
#### Post date: [September 8, 2023, 8:59am UTC](https://community.openai.com/t/an-error-occurred-unexpected-token-doctype-is-not-valid-json/359906/1 "2023-09-08T08:59:34Z")

</div>

When i created an image generator website through open AI APi key all is done after generate image than coming this error "An error occurred: Unexpected token ‘\<’, "\<!DOCTYPE “… is not valid JSON”

---

<div class="post-metadata">

### Author: ![novaphil](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/novaphil/32/130934_2.png) [@novaphil](https://community.openai.com/u/novaphil)
#### Post date: [September 8, 2023, 9:17am UTC](https://community.openai.com/t/an-error-occurred-unexpected-token-doctype-is-not-valid-json/359906/2 "2023-09-08T09:17:38Z")

</div>

Welcome to the forums. Can you share the code that you are using to generate the image?

---

<div class="post-metadata">

### Author: ![akshaymwf](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/akshaymwf/32/146549_2.png) [@akshaymwf](https://community.openai.com/u/akshaymwf)
#### Post date: [September 8, 2023, 9:29am UTC](https://community.openai.com/t/an-error-occurred-unexpected-token-doctype-is-not-valid-json/359906/3 "2023-09-08T09:29:17Z")

</div>

HTML Code

 Generate Image 

 ![Generated Image]()Download Image

javascript code

document.getElementById(“generate-button”).addEventListener(“click”, function() {  
var prompt = document.getElementById(‘prompt’).value.trim();  
var loading = document.getElementById(‘loading’);  
var result = document.getElementById(‘result’);  
var downloadLink = document.getElementById(‘download-link’);

```
if (prompt === '') {
    alert('Please enter a prompt before generating an image.');
    return;
}

loading.style.display = 'block';
result.style.display = 'none';
downloadLink.style.display = 'none';

fetch('generate_image.php', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    },
    body: 'prompt=' + encodeURIComponent(prompt)
})
.then(response => response.json())
.then(data => {
    loading.style.display = 'none';
    result.style.display = 'block';
    downloadLink.style.display = 'block';
    result.src = data.choices[0].image; // Make sure the path is correct
    downloadLink.href = data.choices[0].image; // Make sure the path is correct
})
.catch(error => {
    loading.style.display = 'none';
    alert('An error occurred: ' + error.message);
});

```

});

Php Code

function openai\_generate\_image() {  
// Check if a prompt has been submitted  
if (isset($\_POST[‘prompt’])) {  
prompt = sanitize\_text\_field(\_POST[‘prompt’]);

```
    // Validate the prompt (add more validation as needed)
    if (empty($prompt)) {
        wp_send_json_error('Please enter a prompt.');
    }

    // Your OpenAI API key
    $api_key = 'Your Openai API'; // Replace with your actual API key

    // API endpoint
    $api_url = 'https://api.openai.com/v1/completions';

    // Request data
    $data = array(
        'model' => 'image-alpha-001', // Replace with the correct DALL·E model name
        'prompt' => $prompt,
        'n' => 1
    );

    // Headers for the OpenAI API
    $headers = array(
        'Content-Type' => 'application/json',
        'Authorization' => 'Bearer ' . $api_key
    );

    // Args for the WordPress HTTP API
    $args = array(
        'body' => json_encode($data),
        'headers' => $headers,
        'timeout' => 30
    );

    // Send the request to OpenAI
    $response = wp_safe_remote_post($api_url, $args);

    // Check for errors
    if (is_wp_error($response)) {
        wp_send_json_error('Error connecting to OpenAI: ' . $response->get_error_message());
    }

    // Get the API response
    $body = wp_remote_retrieve_body($response);
    $result = json_decode($body);

    // Check for errors in the OpenAI response
    if (empty($result->choices) || !isset($result->choices[0]->image)) {
        wp_send_json_error('Image generation failed. Please try again.');
    }

    // Get the generated image URL
    $image_url = $result->choices[0]->image;

    // Send success response with the image URL
    wp_send_json_success(array('image_url' => $image_url));
} else {
    // No prompt submitted
    wp_send_json_error('No prompt provided.');
}

```

}

// Add the AJAX action for generating images  
add\_action(‘wp\_ajax\_openai\_generate\_image’, ‘openai\_generate\_image’);  
add\_action(‘wp\_ajax\_nopriv\_openai\_generate\_image’, ‘openai\_generate\_image’);  
?\>

---

<div class="post-metadata">

### Author: ![novaphil](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/novaphil/32/130934_2.png) [@novaphil](https://community.openai.com/u/novaphil)
#### Post date: [September 8, 2023, 9:33am UTC](https://community.openai.com/t/an-error-occurred-unexpected-token-doctype-is-not-valid-json/359906/4 "2023-09-08T09:33:27Z")

</div>

GPT is not good at generating GPT API code because it didn’t exist before the training. You’ve got the wrong endpoint and API call. Take a look at the official API docs, they’re pretty short:

> **[OpenAI Platform](https://platform.openai.com/docs/guides/images/usage?lang=curl)**
>
> Explore developer resources, tutorials, API docs, and dynamic examples to get the most out of OpenAI's platform.

---

<div class="post-metadata">

### Author: ![akshaymwf](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/akshaymwf/32/146549_2.png) [@akshaymwf](https://community.openai.com/u/akshaymwf)
#### Post date: [September 8, 2023, 9:36am UTC](https://community.openai.com/t/an-error-occurred-unexpected-token-doctype-is-not-valid-json/359906/5 "2023-09-08T09:36:15Z")

</div>

PLEASE Tell me main issue which code line can you highlight code are where error

---

<div class="post-metadata">

### Author: ![novaphil](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/novaphil/32/130934_2.png) [@novaphil](https://community.openai.com/u/novaphil)
#### Post date: [September 8, 2023, 9:48am UTC](https://community.openai.com/t/an-error-occurred-unexpected-token-doctype-is-not-valid-json/359906/6 "2023-09-08T09:48:44Z")

</div>

> [@akshaymwf](#):
>
> ```auto
> // API endpoint
> $api_url = 'https://api.openai.com/v1/completions';
> 
> // Request data
> $data = array(
> 'model' => 'image-alpha-001', // Replace with the correct DALL·E model name
> 'prompt' => $prompt,
> 'n' => 1
> );
> 
> ```

That endpoint is for text not images. See the docs.

---

<div class="post-metadata">

### Author: ![akshaymwf](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/akshaymwf/32/146549_2.png) [@akshaymwf](https://community.openai.com/u/akshaymwf)
#### Post date: [September 8, 2023, 9:53am UTC](https://community.openai.com/t/an-error-occurred-unexpected-token-doctype-is-not-valid-json/359906/7 "2023-09-08T09:53:39Z")

</div>

please provide me endpoint for image i have all concept clear but here i confused

---

<div class="post-metadata">

### Author: ![novaphil](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/novaphil/32/130934_2.png) [@novaphil](https://community.openai.com/u/novaphil)
#### Post date: [September 8, 2023, 9:56am UTC](https://community.openai.com/t/an-error-occurred-unexpected-token-doctype-is-not-valid-json/359906/8 "2023-09-08T09:56:54Z")

</div>

Read the API documentation I linked earlier.

---

<div class="post-metadata">

### Author: ![pierredutrout](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/pierredutrout/32/146386_2.png) [@pierredutrout](https://community.openai.com/u/pierredutrout)
#### Post date: [March 18, 2024, 12:12am UTC](https://community.openai.com/t/an-error-occurred-unexpected-token-doctype-is-not-valid-json/359906/9 "2024-03-18T00:12:02Z")

</div>

Hello, I just tried to generate an outpainting frame on an image ON the openai website and got the same error in the info pop-up.
