# How to pass a base64 image to the Infer API endpoint

**URL:** <https://discuss.roboflow.com/t/how-to-pass-a-base64-image-to-the-infer-api-endpoint/5736>\
**Category:** 🤝  Community Help\
**Created:** [May 10, 2024, 3:32pm UTC](https://discuss.roboflow.com/t/how-to-pass-a-base64-image-to-the-infer-api-endpoint/5736 "2024-05-10T15:32:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![george\_bibu](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/george_bibu/32/4553_2.png) [@george\_bibu](https://discuss.roboflow.com/u/george_bibu)\
**Post date:** [May 10, 2024, 3:32pm UTC](https://discuss.roboflow.com/t/how-to-pass-a-base64-image-to-the-infer-api-endpoint/5736/1 "2024-05-10T15:32:14Z")

</div>

[https://docs.roboflow.com/deploy/hosted-api/custom-models/object-detection](https://docs.roboflow.com/deploy/hosted-api/custom-models/object-detection)  
In the documentation for the API, it says to pass base64 images through the body of our POST request.

```auto
response = requests.post(roboflow_url, params=params, data=base64_image)

```

This is the request code and all it returns is error code 400. params is my API key. What do I do?

---

<div class="post-metadata">

**Author:** ![peter\_roboflow](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/peter_roboflow/32/2609_2.png) [@peter\_roboflow](https://discuss.roboflow.com/u/peter_roboflow)\
**Post date:** [May 10, 2024, 5:54pm UTC](https://discuss.roboflow.com/t/how-to-pass-a-base64-image-to-the-infer-api-endpoint/5736/2 "2024-05-10T17:54:44Z")

</div>

Hi @george_bibu , what is the value of roboflow\_url?

Can you make sure that `roboflow_url=f'https://detect.roboflow.com/{project_id}/{version_id}`

---

<div class="post-metadata">

**Author:** ![george\_bibu](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/george_bibu/32/4553_2.png) [@george\_bibu](https://discuss.roboflow.com/u/george_bibu)\
**Post date:** [May 10, 2024, 6:07pm UTC](https://discuss.roboflow.com/t/how-to-pass-a-base64-image-to-the-infer-api-endpoint/5736/3 "2024-05-10T18:07:08Z")

</div>

Yes, that is the value of my roboflow\_url.

---

<div class="post-metadata">

**Author:** ![george\_bibu](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/george_bibu/32/4553_2.png) [@george\_bibu](https://discuss.roboflow.com/u/george_bibu)\
**Post date:** [May 11, 2024, 7:26am UTC](https://discuss.roboflow.com/t/how-to-pass-a-base64-image-to-the-infer-api-endpoint/5736/4 "2024-05-11T07:26:12Z")

</div>

Hello? Were you able to find anything out? I would really like to know if it’s possible to access the endpoint in this manner or not, because I really don’t want to waste time trying to do something that’s impossible.

---

<div class="post-metadata">

**Author:** ![peter\_roboflow](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/peter_roboflow/32/2609_2.png) [@peter\_roboflow](https://discuss.roboflow.com/u/peter_roboflow)\
**Post date:** [May 11, 2024, 2:40pm UTC](https://discuss.roboflow.com/t/how-to-pass-a-base64-image-to-the-infer-api-endpoint/5736/5 "2024-05-11T14:40:37Z")

</div>

Try

```auto
import base64
import os
from PIL import Image
import requests

API_KEY = os.environ["API_KEY"]
IMAGE_PATH = "image.jpg"
PROJECT_ID = ""
VERSION_ID = ""

def encode_bas64(image_path):
    with open(image_path, "rb") as image:
        x = image.read()
        image_string = base64.b64encode(x)

    return image_string.decode("ascii")

def do_request():
    api_key = API_KEY
    infer_payload = {
        "image": {
            "type": "base64",
            "value": encode_bas64(IMAGE_PATH),
        },
        "api_key": api_key,
    }
    roboflow_url = f"https://detect.roboflow.com/{PROJECT_ID}/{VERSION_ID}",
    response = requests.post(roboflow_url, json=infer_payload)
    print(response.json())

```

If that doesn’t work, try using the inference client:

> **[What is Inference? - Roboflow Inference](https://inference.roboflow.com/#run-client)**
>
> Scalable, on-device computer vision deployment.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/1X/bb6edbfa56d0cf2ee603f061e9e09103502cc8cc.png) [@system](https://discuss.roboflow.com/u/system)\
**Post date:** [June 1, 2024, 2:40pm UTC](https://discuss.roboflow.com/t/how-to-pass-a-base64-image-to-the-infer-api-endpoint/5736/6 "2024-06-01T14:40:55Z")

</div>

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.
