PHPackages                             derrickob/gemini-api - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. derrickob/gemini-api

ActiveLibrary[API Development](/categories/api)

derrickob/gemini-api
====================

A lightweight, efficient, custom PHP Client for seamless Google Gemini API integration.

v1.1.2(1y ago)41.9k↓33.3%2[1 issues](https://github.com/derrickobedgiu1/gemini-api/issues)MITPHPPHP ^8.1

Since Aug 11Pushed 1y ago2 watchersCompare

[ Source](https://github.com/derrickobedgiu1/gemini-api)[ Packagist](https://packagist.org/packages/derrickob/gemini-api)[ Docs](https://github.com/derrickobedgiu1/gemini-api)[ RSS](/packages/derrickob-gemini-api/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (10)Versions (5)Used By (0)

[![](files/banner.jpeg)](files/banner.jpeg)*Image Credit: Google DeepMind*

 [![Packagist Version](https://camo.githubusercontent.com/3e2b833328718f70713422df4ed886542dc3b06bad5d3e0a51f1f494ef03e336/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6465727269636b6f622f67656d696e692d617069)](https://packagist.org/packages/derrickob/gemini-api) [![GitHub License](https://camo.githubusercontent.com/4a70c3478cfc0039c9245898f4c1bd5b583fb00526090ff111a561fb7a6a734c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6465727269636b6f626564676975312f67656d696e692d617069)](https://github.com/derrickobedgiu1/gemini-api/blob/main/LICENSE)

Custom PHP Client for Google Gemini API
=======================================

[](#custom-php-client-for-google-gemini-api)

---

Important

**This library is an independent creation that tries to provide access to the latest Google Gemini API features &amp; capabilities. It is neither endorsed nor an official library. If you're looking for Official Google Gemini API libraries, please [Check Here](https://ai.google.dev/gemini-api/docs/downloads)**.

Built with [SaloonPHP](https://github.com/saloonphp/saloon) as Core Dependency.

Table of Contents
-----------------

[](#table-of-contents)

- [\# Custom PHP Client for Google Gemini API](#-custom-php-client-for-google-gemini-api)
- [Table of Contents](#table-of-contents)
- [Releases](#releases)
- [Prerequisites](#prerequisites)
    - [Dependencies](#dependencies)
    - [Set up your API Key](#set-up-your-api-key)
    - [Set Up Service Account](#set-up-service-account)
- [Installation](#installation)
    - [Environment Variables](#environment-variables)
- [Notes](#notes)
- [Getting Started](#getting-started)
    - [API Key](#api-key)
    - [Service Account](#service-account)
    - [Manual Credentials](#manual-credentials)
    - [Proxy](#proxy)
- [Cached Content Resource](#cached-content-resource)
    - [create](#create)
    - [delete](#delete)
    - [get](#get)
    - [list](#list)
    - [patch](#patch)
- [Corpora Resource](#corpora-resource)
    - [create](#create-1)
    - [delete](#delete-1)
    - [get](#get-1)
    - [list](#list-1)
    - [patch](#patch-1)
    - [query](#query)
- [Corpora Document Resource](#corpora-document-resource)
    - [create](#create-2)
    - [delete](#delete-2)
    - [get](#get-2)
    - [list](#list-2)
    - [patch](#patch-2)
    - [query](#query-1)
- [Corpora Document Chunk Resource](#corpora-document-chunk-resource)
    - [batchCreate](#batchcreate)
    - [batchDelete](#batchdelete)
    - [batchUpdate](#batchupdate)
    - [create](#create-3)
    - [delete](#delete-3)
    - [get](#get-3)
    - [list](#list-3)
    - [patch](#patch-3)
- [Corpora Permission Resource](#corpora-permission-resource)
    - [create](#create-4)
    - [delete](#delete-4)
    - [get](#get-4)
    - [list](#list-4)
    - [patch](#patch-4)
- [File Resource](#file-resource)
    - [delete](#delete-5)
    - [get](#get-5)
    - [list](#list-5)
- [Media Resource](#media-resource)
    - [upload](#upload)
- [Model Resource](#model-resource)
    - [batchEmbedContents](#batchembedcontents)
    - [batchEmbedText](#batchembedtext)
    - [countMessageTokens](#countmessagetokens)
    - [countTextTokens](#counttexttokens)
    - [countTokens](#counttokens)
    - [embedContent](#embedcontent)
    - [embedText](#embedtext)
    - [generateContent](#generatecontent)
    - [generateMessage](#generatemessage)
    - [generateText](#generatetext)
    - [get](#get-6)
    - [list](#list-6)
- [Tuned Model Resource](#tuned-model-resource)
    - [create](#create-5)
    - [delete](#delete-6)
    - [generateContent](#generatecontent-1)
    - [get](#get-7)
    - [list](#list-7)
    - [patch](#patch-5)
    - [transferOwnership](#transferownership)
- [Tuned Model Operation Resource](#tuned-model-operation-resource)
    - [cancel](#cancel)
    - [get](#get-8)
    - [list](#list-8)
- [Tuned Model Permission Resource](#tuned-model-permission-resource)
    - [create](#create-6)
    - [delete](#delete-7)
    - [get](#get-9)
    - [list](#list-9)
    - [patch](#patch-6)
- [Tests](#tests)
- [Support Project](#support-project)

Releases
--------

[](#releases)

This library uses the beta version of the Gemini API by default to try and ensure access to the most recent functionalities.

The versioning scheme follows the format `v1.x.y`:

1. `y` is incremented for patches and backward-compatible bug fixes.
2. `x` is incremented for new features or changes that may include breaking changes.

To ensure stability **with this library**, you may consider locking your composer version to a specific release. Update your package only after reviewing the changelog and ensuring you can accommodate any breaking changes in the new release. This, however, won't apply for any upstream breaking changes if you're using the default (`v1beta`) API version.

Since there may be occasional breaking changes from the beta version, this library may end up having more frequent `x` version increments to adapt to these changes, mostly for existing broken functionalities it previously implemented.

You can also lock your usage to `v1` by explicitly defining it when constructing the Gemini class, keeping note of which features are available in `v1` and those in `v1beta`. There's a high chance you'll also get errors if the `v1` API receives request containing `v1beta`-specific parameters (which fully are), so using `v1beta` might seem mandatory with this library as the entire development was based on `v1beta` syntax except tuned model operations that route to `v1`.

Prerequisites
-------------

[](#prerequisites)

### Dependencies

[](#dependencies)

1. PHP 8.1+
2. [Composer](https://getcomposer.org/)

### Set up your API Key

[](#set-up-your-api-key)

To use the Gemini API, you'll need an API key. If you don't already have one, create a key in Google AI Studio. [Get an API Key](https://aistudio.google.com/app/apikey)

### Set Up Service Account

[](#set-up-service-account)

1. Go to the [Google Developer Console](https://console.cloud.google.com/) and create a new project if you don't already have one.
2. In the navigation menu, select **APIs &amp; Services &gt; Credentials**.
3. Click on **Create credentials** and select **Service account**.
4. Enter a name for the service account and click **Create**.
5. Assign the necessary roles to your service account and click **Continue**.
6. Click on **Create Key**, choose the **JSON** key type, and click **Create**.
7. Your new public/private key pair will be downloaded to your computer; this is your service accountkey.
8. Securely store the JSON file containing the key, as it allows access to the Gemini API. You'll provide the path to it in your code.

Installation
------------

[](#installation)

```
composer require derrickob/gemini-api
```

For caching access token requests from your service account JSON file, use any PSR-6 compatible cache library such as `symphony/cache` (requires PHP &gt;= 8.2).

```
composer require symfony/cache
```

### Environment Variables

[](#environment-variables)

Set your environment variables:

*Powershell*

```
$Env:GOOGLE_API_KEY = "AIza...."
```

*Cmd*

```
set GOOGLE_API_KEY=AIza....
```

*Linux or macOS*

```
export GOOGLE_API_KEY=AIza....
```

Notes
-----

[](#notes)

1. **API Syntax and Naming:** This library simplifies some aspects of the official API syntax and renames certain methods to avoid conflicts. When writing full requests which don't use the simplified methods, refer to the official documentation alongside the library equivalent class names to ensure accuracy. Only the few renamed classes should prevent you from writing your own documentation-led request if the simplified versions are incomplete/sub-standard for you.
2. **Query Parameters:** Any query parameters not explicitly shown in the examples (e.g., `pageSize`, `pageToken`) can be passed as an associative array within the request. For instance: `$gemini->models()->list(['pageSize' => 5, 'pageToken' => 'the-token']);`

**If the class names are complex to build a request, you can always build using array and passing to their `fromArray` static method (available in all classes).** The JSON Mode example addon in the `examples/` folder uses this. Note that the field data you're passing should be compatible with the expected syntax and data of the class whose `fromArray` you're trying to use.

Getting Started
---------------

[](#getting-started)

### API Key

[](#api-key)

The example demonstrates an endpoint request that uses an API key for authentication.

```
