2

When calling a Stack Exchange API we need to specify which site we are trying to fetch the data from in a API URL query parameter (site=webapps.meta) like below:

https://api.stackexchange.com/docs/answers-on-questions#ids=4882&filter=default&site=webapps.meta

This is not consistent across sites. For example:

Site Site URL query parameter
Stack Overlow https://stackoverflow.com/ site=stackoverflow
Meta Stack Exchange https://meta.stackexchange.com/ site=meta
Web Application Meta https://webapps.meta.stackexchange.com/ site=webapps.meta
MathOverflow https://mathoverflow.net/ site=mathoverflow.net

My questions is, is this documented anywhere on what should be the site query parameter for the respective site?

2
  • 4
    From the docs in paragraph "Per-Site Methods" Each of these methods operates on a single site at a time, identified by the site parameter. This parameter can be the full domain name (ie. "stackoverflow.com"), or a short form identified by api_site_parameter on the site object. So https://api.stackexchange.com/2.3/answers?order=desc&sort=activity&site=webapps.meta.stackexchange.com works as well
    – rene
    Jan 4 at 12:33
  • Oh, this is good input @rene. Thank you for sharing this. This will be the straight-forward way to construct a query. Would you mind adding this as an answer?
    – Gangula
    Jan 4 at 14:07

4 Answers 4

7

You can query this using the API:

https://api.stackexchange.com/2.3/sites

Look for the api_site_parameter property in the output.

Find the documentation at Usage of /sites and the types it returns at Type Site.

5

What @rene says in the comments is absolutely the best advice:

From the docs in paragraph "Per-Site Methods" Each of these methods operates on a single site at a time, identified by the site parameter. This parameter can be the full domain name (ie. "stackoverflow.com"), or a short form identified by api_site_parameter on the site object. So https://api.stackexchange.com/2.3/answers?order=desc&sort=activity&site=webapps.meta.stackexchange.com works as well

I never heeded that advice, so I ended up with lots of exceptional cases in my scripts. Well, I probably needed such type of code anyway; try to guess the pattern for the iOS favicons for all sites from this URL: https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon.png. And there's another system in the landscape with sometimes surprising identifiers; I posted about that here: How can I get URLs and site names for a cross-site SEDE query?

While @rene's solution is definitely the best way to go forward, if you want to insist on the original parameters:

  1. For the main Stack Overflow site, use stackoverflow
  2. If the host ends with .stackexchange.com, use its prefix
  3. If the host ends with .stackoverflow.com, use its prefix and add .stackoverflow
  4. Strip the .com suffix from the host (if it's present), and use the result.

Or use the one-liner from @Makyen's answer, since it's much easier. I tried to reverse engineer an algorithm based on a piece of code which (partially) does the opposite - no wonder it's not optimised.

2
  • 1
    FYI: the steps you've listed are equivalent to domain.replace('.stackexchange.com', '').replace('.com', '') in all cases for the canonical domain names. If you simplify your steps, then I can just delete my answer. :;
    – Makyen
    Jan 4 at 18:00
  • Oh, you're absolutely right, that's what you get when you try to reverse engineer an algorithm based on a piece of code which (partially) does the opposite :D
    – Glorfindel Mod
    Jan 4 at 18:17
2

As rene said in a comment, the documentation, under the "Per-Site Methods" heading, says:

Each of these methods operates on a single site at a time, identified by the site parameter. This parameter can be the full domain name (ie. "stackoverflow.com"), or a short form identified by api_site_parameter on the site object.

Getting the api_site_parameter from the /sites SE API endpoint

You can use the api.stackexchange.com site to compose test requests. For the /sites endpoint, a request which gets the site_url, api_site_parameter, and name for all sites can be seen here.

Computing the api_site_parameter from the site's canonical domain name

In addition, it's possible to compute the api_site_parameter value from the site's domain with simple string replacement. The following is valid code to get the api_site_parameter from the site's canonical domain name in at least Python and JavaScript. For other languages, adjust to the appropriate syntax:

domain.replace('.stackexchange.com', '').replace('.com', '')

will give you the api_site_parameter for all existing sites when using the canonical domain for the site, not an alias.

You can see site aliases with this SE API request. If you want to be fully complete, then you'll need to do a lookup based on the domain name, including all the aliases. However, most of the code I've seen ignores the aliases.

2
  • sigh ... ninja'd :; This answer was made largely redundant by Glorfindel's answer, which I didn't notice was posted while I was composing this one. I'm leaving it up, as the string replacement steps are useful.
    – Makyen
    Jan 4 at 17:24
  • 2
    Yeah, those Stack Apps mods ruin all your fun ...
    – rene
    Jan 4 at 17:45
-1

EDIT:

Erik has pointed out in the comments that the list is paginated so the below list is not the complete list. You can use the pagesize=5000 as suggested to get all the data. I have done the same thing in the following excel using the Excel Power Query to fetch data and create a table (for future reference).

Note: Download the excel and refresh the Power Query to fetch the data again. But do note that refreshing the Power Query consumes your API quota (max 300)


Thanks to @Erik's answer, below is the list of site names to be used in API extracted from the API - https://api.stackexchange.com/2.3/sites

Site Name Site URL api_site_parameter
Stack Overflow https://stackoverflow.com stackoverflow
Server Fault https://serverfault.com serverfault
Super User https://superuser.com superuser
Meta Stack Exchange https://meta.stackexchange.com meta
Web Applications https://webapps.stackexchange.com webapps
Web Applications Meta https://webapps.meta.stackexchange.com webapps.meta
Arqade https://gaming.stackexchange.com gaming
Arqade Meta https://gaming.meta.stackexchange.com gaming.meta
Webmasters https://webmasters.stackexchange.com webmasters
Webmasters Meta https://webmasters.meta.stackexchange.com webmasters.meta
Seasoned Advice https://cooking.stackexchange.com cooking
Seasoned Advice Meta https://cooking.meta.stackexchange.com cooking.meta
Game Development https://gamedev.stackexchange.com gamedev
Game Development Meta https://gamedev.meta.stackexchange.com gamedev.meta
Photography https://photo.stackexchange.com photo
Photography Meta https://photo.meta.stackexchange.com photo.meta
Cross Validated https://stats.stackexchange.com stats
Cross Validated Meta https://stats.meta.stackexchange.com stats.meta
Mathematics https://math.stackexchange.com math
Mathematics Meta https://math.meta.stackexchange.com math.meta
Home Improvement https://diy.stackexchange.com diy
Home Improvement Meta https://diy.meta.stackexchange.com diy.meta
Meta Super User https://meta.superuser.com meta.superuser
Meta Server Fault https://meta.serverfault.com meta.serverfault
Geographic Information Systems https://gis.stackexchange.com gis
Geographic Information Systems Meta https://gis.meta.stackexchange.com gis.meta
TeX - LaTeX https://tex.stackexchange.com tex
TeX - LaTeX Meta https://tex.meta.stackexchange.com tex.meta
Ask Ubuntu https://askubuntu.com askubuntu
Ask Ubuntu Meta https://meta.askubuntu.com meta.askubuntu
6
  • 2
    Note that there are many more sites than that, that request is paginated. For the unpaginated variant, pass a large pagesize: api.stackexchange.com/2.3/sites?pagesize=50000. But listing them all in an answer doesn't really seem sensible to me.
    – Erik A
    Jan 4 at 12:50
  • Oh, is there any other API to fetch the site details of a specific site then?
    – Gangula
    Jan 4 at 13:29
  • 1
    You know there are docs for that endpoint, right? api.stackexchange.com/docs/sites which return a list of sites: api.stackexchange.com/docs/types/site.
    – rene
    Jan 4 at 13:35
  • @Gangula Of course, the /info endpoint. It requires a filter to get the api_site_parameter, e.g. api.stackexchange.com/2.3/…
    – Erik A
    Jan 4 at 13:37
  • But to use the /info API, we need to know the api_site_parameter right?
    – Gangula
    Jan 4 at 13:45
  • Or the URL, since you can just use that, as rene indicated in his comment on your question. No clue how you'd want to fetch info on a specific site without knowing the URL.
    – Erik A
    Jan 4 at 15:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .