Giter Club home page Giter Club logo

suno-api's Introduction

Suno AI API

GitHub PyPI

Suno AI API is

  • An unofficial Python library for Suno AI API

Suno AI API supports to

  • Create a Python client for Suno AI
  • Utilize Chirp v3 model to make a song by default
  • Continue from a song
  • Make a song using CLI
  • Deploy a REST API available at http://http://127.0.0.1/:8000
  • Deploy with Docker
  • Deploy on Vercel

Installation

pip install suno-api

or

git clone [email protected]:imyizhang/suno-api.git
cd suno-api
poetry install --only main

Quickstart

Sign in to Suno AI at https://app.suno.ai/, and get your cookie

You can find your cookie from the browser's Developer Tools -> Network tab

Screenshot

Create a client for Suno AI with your cookie

import suno

client = suno.Suno(cookie="your-cookie-here")

Create your clips

clips = client.songs.generate(
    "your-song-description-here", 
    instrumental=False,
)

or start with custom mode

clips = client.songs.generate(
    "your-lyrics-here", 
    cutomized=True,
    tags="your-music-style-here",
    instrumental=False,
)

Review your newly created clip

clip = client.songs.get("your-clip-id-here")

Review all your created clips in the library

clips = client.songs.list()

Check your remaining credits

credits = client.get_credits()

Download a clip on Suno AI

suno.download("your-clip-id-here")

Documentation

Python Library

suno.Song

suno.Song(*args, **kwargs)

An object representing a song.

Properties:

id (str): Unique ID for the song.

video_url (str): Video URL for the song.

audio_url (str): Audio URL for the song.

image_url (str | None): Image URL for the song.

image_large_url (str | None): Large image URL for the song.

major_model_version (str): Major model version used to create the song.

model_name (str): Model name used to create the song.

metadata (dict): Metadata of the song.

is_liked (bool): The song is liked or not.

user_id (str): Unique ID for a user who created the song.

is_trashed (bool): The song is trashed or not.

reaction (dict | None): Reaction to the song.

created (str): When the song was created.

status (str): Status for the song.

title (str): Title for the song.

play_count (int): Play count for the song.

upvote_count (int): Upvote count for song.

is_public (bool): The song is public or not.

suno.Suno

suno.Suno(cookie: str)

A object representing a client for Suno AI.

Parameters:

  • cookie (str): Cookie stored for the cookie-based authentication.

Properties:

headers (dict): Request headers.

songs.generate(prompt: str, custom: bool, tags: str, instrumental: bool)

Create songs.

Each song generation consumes 5 credits, thus a total of 10 credits is necessary for each successful call.

Parameters:

  • prompt (str): Prompt used to create the song, song, description, lyrics, or style of music.
  • custom (bool, optional): Whether to create the song in custom mode. Defaults to False.
  • tags (str, optional): Tags indicating musical style of the song to be created. Defaults to "".
  • instrumental (bool, optional): Whether to create the song without lyrics. Defaults to False.

Returns:

(List[suno.Song]): A list of suno.Song objects representing the created songs.

get_songs()

List all songs (equivalent to songs.list()).

Returns:

(List[suno.Song]): A list of suno.Song objects representing the songs in the library of the logged-in account.

get_song(id: str)

Get a song by its ID (equivalent to songs.get(id: str)).

Parameters:

  • id (str): ID of the song.

Returns:

(suno.Song): A suno.Song object representing the song.

get_credits()

Get all credits left.

Returns:

(int): Remaining credits in the logged-in account.

suno.download

suno.download(song: str | suno.Song, root: str)

Download a song.

Parameters:

  • song (str | suno.Song): ID indicating a song, URL linked to a song or an suno.Song object.
  • root (str): Root directory to store the downloaded songs.

REST API

Set environment variable SUNO_COOKIE

Check the value of the environment variable SUNO_COOKIE on macOS
echo $SUNO_COOKIE
Add the environment variable SUNO_COOKIE permanently on macOS

Find your current shell.

echo $0

In zsh, a environment variable can be permanently added to the configuration file ~/.zshrc. In bash, the configuration file is ~/.bash_profile.

vim ~/.zshrc

In the configuration file, add the environment variable SUNO_COOKIE.

export SUNO_COOKIE="your-cookie-here"

Save the changes to the configuration file and execute it.

source ~/.zshrc

Deployment

git clone [email protected]:imyizhang/suno-api.git
cd suno-api/suno
uvicorn api:app --reload

or

git clone [email protected]:imyizhang/suno-api.git
cd suno-api
poetry run python suno/api.py

POST /v1/songs

Create songs.

Request example

cURL

curl -X POST "http://localhost:8000/v1/songs" \
     -H "Content-Type: application/json" \
     -d '{"prompt": "Make a song about the moon"}'

Suno AI CLI

suno songs generate "Make a song about the moon"

Python

import json

import requests

data = json.dumps({
    "prompt": "Make a song about the moon",
})

response = requests.post("http://localhost:8000/v1/songs", data=data)
response.json()

GET v1/songs

List all songs.

Request example

cURL

curl -X GET "http://localhost:8000/v1/songs" \
     -H "Accept: application/json"

Suno AI CLI

suno songs list

Python

import requests

response = requests.get('http://localhost:8000/v1/songs')
response.json()

GET v1/song/{id}

Get a song by its ID.

Request example

cURL

curl -X GET "http://localhost:8000/v1/songs/{id}" \
     -H "Accept: application/json"

Suno AI CLI

suno songs get ID

Python

import requests

response = requests.get('http://localhost:8000/v1/songs/{id}')
response.json()
Response Example
{
  "id":"0e42f167-17ab-4004-941b-d549b24dce76",
  "video_url":"https://cdn1.suno.ai/0e42f167-17ab-4004-941b-d549b24dce76.mp4",
  "audio_url":"https://cdn1.suno.ai/0e42f167-17ab-4004-941b-d549b24dce76.mp3",
  "image_url":"https://cdn1.suno.ai/image_75b03116-0aa2-4367-bdf6-8556dde4df9f.png",
  "image_large_url":"https://cdn1.suno.ai/image_large_75b03116-0aa2-4367-bdf6-8556dde4df9f.png",
  "major_model_version":"v3",
  "model_name":"chirp-v3",
  "metadata":{
    "tags":"Rap\nJapanese rock\nPop\nVocaloid style\nEnergetic\nUpbeat\nCatchy\nCulinary\nCooking\nRecipe-focused",
    "prompt":"[Chorus]\nIn der Weihnachtsbäckerei\nGibt es manche Leckerei\nZwischen Mehl und Milch\nMacht so mancher Knilch\nEine riesengroße Kleckerei\nIn der Weihnachtsbäckerei\nIn der Weihnachtsbäckerei\n\n[Verse 1]\nWo ist das Rezept geblieben\nVon den Plätzchen, die wir lieben?\nWer hat das Rezept verschleppt?\n\"Ich nicht\"\n\"Du vielleicht?\"\n\"Ich auch nicht\"\n\nNa, dann müssen wir es packen\nEinfach frei nach Schnauze backen\nSchmeißt den Ofen an (oh ja)\nUnd ran\n\n[Chorus]\nIn der Weihnachtsbäckerei\nGibt es manche Leckerei\nZwischen Mehl und Milch\nMacht so mancher Knilch\nEine riesengroße Kleckerei\nIn der Weihnachtsbäckerei\nIn der Weihnachtsbäckerei\n\n[Verse 2]\nBrauchen wir nicht Schokolade\nHonig, Nüsse und Succade\nUnd ein bischen Zimt?\nDas stimmt\n\nButter, Mehl und Milch verrühren\nZwischendurch einmal probieren\nUnd dann kommt das Ei (pass auf)\nVorbei\n\n[Chorus]\nIn der Weihnachtsbäckerei\nGibt es manche Leckerei\nZwischen Mehl und Milch\nMacht so mancher Knilch\nEine riesengroße Kleckerei\nIn der Weihnachtsbäckerei\nIn der Weihnachtsbäckerei\n\n[Verse 3]\nBitte mal zur Seite treten\nDenn wir brauchen Platz zum kneten\nSind die Finger rein?\nDu Schwein\n\n\nSind die Plätzchen, die wir stechen\nErstmal auf den Ofenblechen\nWarten wir gespannt\nVerbrannt\n\n[Chorus]\nIn der Weihnachtsbäckerei\nGibt es manche Leckerei\nZwischen Mehl und Milch\nMacht so mancher Knilch\nEine riesengroße Kleckerei\nIn der Weihnachtsbäckerei\nIn der Weihnachtsbäckerei\n\n[Outro]",
    "gpt_description_prompt":"None",
    "audio_prompt_id":"None",
    "history":"None",
    "concat_history":[
      {
        "id":"7b9cfa43-d231-44e8-b69a-d9c0627b016d",
        "continue_at":107.0
      },
      {
        "id":"75b03116-0aa2-4367-bdf6-8556dde4df9f",
        "continue_at":"None"
      }
    ],
    "type":"concat",
    "duration":158.83997916666667,
    "refund_credits":"None",
    "stream":"None",
    "error_type":"None",
    "error_message":"None"
  },
  "is_liked":false,
  "user_id":"cb7486d0-238e-409e-9ff9-e69db325fa84",
  "is_trashed":false,
  "reaction":"None",
  "created_at":"2024-03-24T13:46:25.916Z",
  "status":"complete",
  "title":"In der Weihnachtsbäckerei ",
  "play_count":0,
  "upvote_count":724,
  "is_public":true
}

GET v1/credits

Get all credits left.

Request example

cURL

curl -X GET "http://localhost:8000/v1/credits" \
     -H "Accept: application/json"

Suno AI CLI

suno credits display

Python

import requests

response = requests.get('http://localhost:8000/v1/credits')
response.json()
Response Example
{
  "total_credits_left": 50
}

License

Suno AI API has a BSD-3-Clause license, as found in the LICENSE file.

Contributing

Thanks for your interest in contributing to Suno AI API! Please feel free to create a pull request.

Changelog

Suno AI API 0.1.2

  • Add a function to download any song on Suno AI
  • Fix bugs for song generation

Suno AI API 0.1.1

  • Introduce suno.Song to structure suno.Suno methods' outputs
  • Support environment variable SUNO_COOKIE
  • Support CLI for Suno AI API
  • Allow to deploy REST API for Suno AI API

Suno AI API 0.1.0

  • Quickly generate a song with a description
  • Create songs with lyrics and music style in custom mode
  • Review songs using their ID
  • List all songs in the library

suno-api's People

Contributors

imyizhang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

suno-api's Issues

curl_cffi.requests.errors.RequestsError: impersonate chrome is not supported

I'm trying to get this to run but I get this error:

File "/opt/homebrew/lib/python3.11/site-packages/suno/suno.py", line 27, in init
self._sid = self._get_sid()
^^^^^^^^^^^^^^^
File "/opt/homebrew/lib/python3.11/site-packages/suno/suno.py", line 32, in _get_sid
response = super().request("GET", url)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/lib/python3.11/site-packages/suno/suno.py", line 20, in request
return self._session.request(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/lib/python3.11/site-packages/curl_cffi/requests/session.py", line 618, in request
req, buffer, header_buffer, q, header_recved, quit_now = self._set_curl_options(
^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/lib/python3.11/site-packages/curl_cffi/requests/session.py", line 382, in _set_curl_options
raise RequestsError(f"impersonate {impersonate} is not supported")
curl_cffi.requests.errors.RequestsError: impersonate chrome is not supported.

I am running curl_cffi at 0.6.0.

ask

can i ask what use of this? can create multiple song and more than 2mins?

Can't create client with a cookie. HTTP 403 response

The line command suno.Suno(cookie=suno_cookie) always returns the following exception for me:

---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
Cell In[16], [line 2](vscode-notebook-cell:?execution_count=16&line=2)
      [1](vscode-notebook-cell:?execution_count=16&line=1) # Create a client
----> [2](vscode-notebook-cell:?execution_count=16&line=2) client = suno.Suno(cookie=suno_cookie)
      [3](vscode-notebook-cell:?execution_count=16&line=3) credits = client.get_credits()
      [4](vscode-notebook-cell:?execution_count=16&line=4) print(f"credits before song generation: {credits} ")

File [c:\Users\***\AppData\Local\Programs\Python\Python312\Lib\site-packages\suno\suno.py:78](file:///C:/Users/***/AppData/Local/Programs/Python/Python312/Lib/site-packages/suno/suno.py:78), in Suno.__init__(self, cookie)
     [76](file:///C:/Users/***/AppData/Local/Programs/Python/Python312/Lib/site-packages/suno/suno.py:76)     raise Exception("environment variable SUNO_COOKIE is not set")
     [77](file:///C:/Users/***/AppData/Local/Programs/Python/Python312/Lib/site-packages/suno/suno.py:77) super().__init__(cookie)
---> [78](file:///C:/Users/***/AppData/Local/Programs/Python/Python312/Lib/site-packages/suno/suno.py:78) self._sid = self._get_sid()
     [79](file:///C:/Users/***/AppData/Local/Programs/Python/Python312/Lib/site-packages/suno/suno.py:79) self.songs = Songs(self)

File [c:\Users\***\AppData\Local\Programs\Python\Python312\Lib\site-packages\suno\suno.py:85](file:///C:/Users/***/AppData/Local/Programs/Python/Python312/Lib/site-packages/suno/suno.py:85), in Suno._get_sid(self)
     [83](file:///C:/Users/***/AppData/Local/Programs/Python/Python312/Lib/site-packages/suno/suno.py:83) response = super().request("GET", url)
     [84](file:///C:/Users/***/AppData/Local/Programs/Python/Python312/Lib/site-packages/suno/suno.py:84) if not response.ok:
---> [85](file:///C:/Users/***/AppData/Local/Programs/Python/Python312/Lib/site-packages/suno/suno.py:85)     raise Exception(f"failed to get SID: {response.status_code}")
     [86](file:///C:/Users/***/AppData/Local/Programs/Python/Python312/Lib/site-packages/suno/suno.py:86) data = response.json()
     [87](file:///C:/Users/***/AppData/Local/Programs/Python/Python312/Lib/site-packages/suno/suno.py:87) return data.get("response").get("last_active_session_id")

Exception: failed to get SID: 403

I got my cookie from the Request "https://clerk.suno.com/v1/client?_clerk_js_version=4.72.0-snapshot.vc141245" out of the Chrome browser, which seems to be what the screenshot in the quickstart documentation suggests. I have tried Cookies from other requests too and made sure that they were not older that a minute or so.
I have updated suno-api to version 0.1.2, but that did not fix the problem.

The problematic request seems to be a GET "https://clerk.suno.ai/v1/client?_clerk_js_version=4.70.5". When I open this URL in the browser, it is blocked by Cloudflare ("The action you just performed triggered the security solution. "). When I open https://clerk.suno.com/v1/client?_clerk_js_version=4.72.0-snapshot.vc141245 instead, I get a valid response. So the version-id in the URL seems to be the problem. Not sure if this is only a temporary issue, but it persists for some days now.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.