Creating a REST API with Pinecone: Step by Step
We’re building a REST API with Pinecone that allows you to interact with vector data effortlessly. This approach is crucial for those looking to implement machine learning models and indexed data without getting lost in the weeds.
Prerequisites
- Python 3.11+
- pip install pinecone-io
- Basic understanding of RESTful APIs
Step 1: Setting Up Your Environment
First, we’ll need to set up Python and install the Pinecone client. Make sure you have the right version of Python. The latest Pinecone Python client can be found on GitHub:
pip install pinecone-client
Install it and verify the installation with the following command:
pip show pinecone-client
If you see an error saying the package isn’t found, likely you’re in a virtual environment that lacks the correct library or your Python version is incompatible. Install the correct version or activate the right environment.
Step 2: Initialize Pinecone
To use Pinecone, we must initialize it. You’ll need an API key which you can find in your Pinecone dashboard. Here’s how to connect:
import pinecone
pinecone.init(api_key="YOUR_API_KEY", environment="us-west1-gcp")
Don’t forget to replace YOUR_API_KEY with your actual Pinecone API key. If this line raises an exception, check if your API key is correct or if you’ve mistakenly set up your environment in the wrong region.
Step 3: Creating an Index
Now we need to create an index. An index is where our vectors will be stored. Specify the index name and dimension of the vectors:
index_name = "example-index"
pinecone.create_index(index_name=index_name, dimension=128)
Make sure to choose a dimension that matches the length of your vector embeddings. If you try to create an index with the wrong dimensions, you’ll see an error that says “Dimension mismatch”.
Step 4: Inserting Data into the Index
After our index is set, let’s insert some data. For demonstration, we’re inserting three vectors:
index = pinecone.Index(index_name)
# Example data: (id, vector)
data = [
("vec1", [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]),
("vec2", [0.2, 0.3, 0.1, 0.5, 0.6, 0.8, 0.9, 0.7, 0.1, 0.4, 0.2, 0.3, 0.5, 0.2, 0.1, 0.5, 0.3, 0.2, 0.1, 0.4, 0.6, 0.3, 0.5, 0.1, 0.8, 0.5, 0.4, 0.3, 0.2, 0.1, 0.5, 0.9, 0.6, 0.7, 0.3, 0.9, 0.5, 0.2],
("vec3", [0.3, 0.4, 0.1, 0.2, 0.5, 0.6, 0.1, 0.7, 0.8, 0.9, 0.4, 0.2, 0.6, 0.7, 0.8, 0.5, 0.3, 0.2, 0.3, 0.1, 0.9, 0.8, 0.5, 0.4, 0.7, 0.1, 0.3, 0.2, 0.4, 0.1, 0.5, 0.6, 0.9, 0.2, 0.3, 0.5, 0.8, 0.7)
]
index.upsert(items=data)
If you attempt to upsert data with vectors that aren’t the correct length, you’ll see an error complaining about vector size. Always double-check your dimensions.
Step 5: Creating the API Endpoints
Finally, we need a Flask application to expose our Pinecone operations as REST API endpoints. Here’s how to set up a simple API:
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/vectors', methods=['POST'])
def add_vectors():
vectors = request.json.get('vectors')
index.upsert(items=vectors)
return jsonify({"status": "success", "message": "Vectors added"}), 201
@app.route('/vectors/', methods=['GET'])
def get_vector(id):
vector = index.fetch(ids=[id])
return jsonify({"vector": vector}), 200
if __name__ == "__main__":
app.run(debug=True)
With these routes, you can add vectors through a POST request and retrieve them with a GET request. Be sure to include content-type headers when testing with tools like Postman. If you forget this, your application will throw an error for missing data, so don’t skip it.
The Gotchas
- API Key Exposure: It’s easy to accidentally push your code to a public repository with your API key. Use environment variables to store sensitive data securely.
- Pinecone Index Limits: Different tiers of Pinecone subscriptions have limits on the number of indexes you can create. Keep track!
- Error Handling: Properly handle errors in your Flask app. If you don’t, your UI might break without an indication of what went wrong.
- Vector Sizing: Always ensure your vector dimensions match your index dimensions. This will save hours of debugging.
Full Code
import pinecone
from flask import Flask, request, jsonify
# Step 1: Initialize Pinecone
pinecone.init(api_key="YOUR_API_KEY", environment="us-west1-gcp")
index_name = "example-index"
pinecone.create_index(index_name=index_name, dimension=128)
# Step 2: Create Flask app
app = Flask(__name__)
index = pinecone.Index(index_name)
@app.route('/vectors', methods=['POST'])
def add_vectors():
vectors = request.json.get('vectors')
index.upsert(items=vectors)
return jsonify({"status": "success", "message": "Vectors added"}), 201
@app.route('/vectors/', methods=['GET'])
def get_vector(id):
vector = index.fetch(ids=[id])
return jsonify({"vector": vector}), 200
if __name__ == "__main__":
app.run(debug=True)
What’s Next
Now that you have a working REST API, the next logical step is to integrate a frontend to call this API and visualize the data. Consider using a simple React app or any modern frontend framework that allows you to make API calls.
FAQ
- What if my vectors do not appear in the index?
Ensure that the insertion request is correctly formatted and check the server logs for errors. - Can I store different vector dimensions?
No, all vectors in a Pinecone index must have the same dimension. - How do I delete vectors?
Use the index’s delete method with the vector ID to remove specific entries.
Data Sources
Check out the official Pinecone documentation on GitHub for more details: pinecone-io/pinecone-python-client. For API specifics, visit the Pinecone Community forum.
| Repository | Stars | Forks | Open Issues | License | Last Updated |
|---|---|---|---|---|---|
| pinecone-io/pinecone-python-client | 422 | 117 | 43 | Apache-2.0 | 2026-03-17 |
Last updated March 26, 2026. Data sourced from official docs and community benchmarks.
🕒 Published: